Python stringStrings are the most commonly used data types in Python. We can use quotation marks ( ' or " ) to create a string. Creating a string is as simple as assigning a value to a variable. E.g:
The above example execution results:
var1[0 Span>]: W Var2[1:8]: welookups
Python string update
You can intercept a part of a string and splicing it with other fields, as in the following example:
Instance (Python 3.0+)
The above example execution results
updated strings : Hello welookups !
Python escape character
Python uses a backslash (\) escape character when you need to use special characters in characters. As shown below:
|\(at the end of the line)||Continuous character|
|\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 table instance variable a has the value string "Hello" and the b variable value is "Python":
|+||String connection||a + b Output: HelloPython|
|*||Repeat output string||a*2 Output: HelloHello|
|||Get the characters in the string by index||a Output e|
|[ : ]||Take a section of the string and follow the Left Closed Right principle, str[0,2] does not contain the third character.||a[1:4] Output ell|
|in||Member operator - Returns True if the string contains the given character||'H' in a Outputs True Td>|
|not in||member operator - returns True if the string does not contain the given character||'M' not in a output True|
|r/R||Original String - Original String: All strings are used literally, without special characters that can be escaped or not printed. The original string has almost exactly the same syntax as a normal string, except that the letter r (which can be capitalized) is preceded by the first quotation mark in the string.||
print( r'\n' ) print( R'\n' )
|%||Format string||Please see the next section.|
The output of the above example is:
a + b Output result： HelloPython a * 2 Output result： HelloHello a Output result： e a[1:4] Output result： ell H In the variable a in M Not in variable a in \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.
Instance (Python 3.0+)
The output of the above example:
My name is welookups this year 10 year old!
python string formatting symbol:symbol number
Format operator helper instructions:
|*||Define width or decimal precision|
|-||Use left alignment|
|+||Show the plus sign ( + )|
|<sp>||Show spaces before positive numbers|
|#||displays zero ('0') in front of the octal number and '0x' or '0X' in front of the hexadecimal (depending on whether it is 'x' or 'X')|
|0||The displayed number is padded with '0' instead of the default space|
|%||'%%' outputs a single '%'|
|(var)||Map Variables (Dictionary Parameters)|
|m.n.||m is the minimum total width of the display, and n is the number of digits after the decimal point (if available)|
Beginning with Python 2.6, a new function for formatting strings has been added.
Beginning with Python 2.6, a new function for formatting strings has been added
The above example execution result is:
This is an instance of a multi-line string Multiple-line strings can use tabs TAB ( ). You can also use line breaks [ ].
Three quotes free the programmer from the quagmire of quotes and special strings, keeping a small string of formatting from start to finish in the so-called WYSIWYG (what you see is what you get) format.
A typical use case is that when you need a piece of HTML or SQL, then string combination, special string escaping will be very cumbersome.
In Python 2, normal strings are stored in 8-bit ASCII, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u.
In Python 3, all strings are Unicode strings.