Python string

Strings are the most commonly used data types in Python. We can use quotation marks ('or") to create strings.

Creating a string is as simple as assigning a value to a variable. For example:

var1 = 'Hello World!'
Var2 = "Python Welookups"

Value in Python access string

Python does not support single-character types, and single characters are also used as a string in Python.

Python accesses substrings, and you can use square brackets to intercept strings, as in the following example:

Instance (Python 2.0+)

#!/usr/bin/python var1 = 'Hello World!' var2= "Python Welookups" print "var1[0]: ", var1[0] print "var2[1:5]: ", var2[1:5]

The above example execution results:

var1[0]:  H
var2[1:5]:  ytho

Python string update

You can modify an existing string and assign it to another variable, as in the following example:

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- var1 = 'Hello World!' print "Update string:- ", var1[:6] + 'Welookups!'

The above example execution results

Update String :-  Hello Welookups !

Python escape character

Python uses a backslash (\) escape character when you need to use special characters in characters. As shown below:

escape character description
\(at the end of the line) Continuous character
\\ backslash symbol
\' single quotes
\" double quotes
\a Bell
\b Backspace
\e escape
\000 empty
\n Line break
\v Vertical Tabs
\t Horizontal tabs
\r Enter
\f Change page
\oyy octal number, the character represented by yy, for example: \o12 stands for line break
\xyy hexadecimal number, the character represented by yy, for example: \x0a stands for line break
\other Other characters are output in normal format

Python string operator

The following example variable a value is the string "Hello" and the b variable value is "Python":

OperatorDescriptionInstance
+string connection
>>>a + b 'HelloPython'
*Repeat output string
>>>a * 2 'HelloHello'
[]Get the characters in a string by index
>>>a[1] 'e'
[ : ]Intercept a part of a string
>>>a[1:4] 'ell'
inMember operator - if the string contains the given character returned True
>>>"H" in a True
not in Member operator - if the string does not contain the given character returned True
>>>"M" not in a True
r/ROriginal String - Original String: All strings are used literally, without special characters that can be escaped or not printed. The original string has almost the same syntax as a normal string except that it is preceded by the letter "r" (which can be capitalized) before the first quote of the string.
>>>print r'\n' \n >>> print R'\n' \n
%Format string Please see the next chapter

Instance(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = "Hello" b = "Python" print "a + b Output result:", a + b print "a * 2 Output result:", a * 2 print "a[1] Output result:", a[1] print "a[1:4] Output result:", a[1:4] if( "H" in a) : print "H In the variable a 中" else : print "H Not in variable a 中" if( "M" not in a) : print "M Not in variable a 中" else : print "M In the variable a 中" print r'\n' print R'\n'

The above program execution result is:

a + b Output result: HelloPython
a * 2 Output result: HelloHello
a[1] Output result: e
a[1:4] Output result: ell
H In the variable a 
M In the variable a 
\n
\n

Python string formatting

Python supports the output of formatted strings. Although this may use very complex expressions, the most basic use is to insert a value into a string with the string formatter %s.

In Python, string formatting uses the same syntax as the sprintf function in C.

Example below:

#!/usr/bin/python

print "My name is %s and weight is %d kg! " %  ('Zara', 21) 

The output of the above example:

My name is Zara and weight is 21< /span> kg!

python string formatting symbol:






welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition