Python string operator
The following table instance variable a value is the string "Hello" and the B variable value is "Python":
operator |
Description |
Example |
+ |
String connection |
A + B output result: Hellopython |
* |
Repeating output string |
A*2 Output Result: Hellohello |
[] |
Getting characters in a string by index |
A[1] Output result e |
[ : ] |
Intercept part of a string |
A[1:4] Output results ell |
Inch |
Member operator-Returns TRUE if the string contains the given character |
' H ' in a output result 1 |
Not in |
Member operator-Returns TRUE if the string does not contain the given character |
' M ' not in a output result 1 |
R/r |
Raw string-Raw string: all strings are used directly as literal meanings, without escaping special or non-printable characters. The original string has almost exactly the same syntax as a normal string, except that the first quotation mark of the string is preceded by the letter R (which can be case). |
Print( R '\ n ')print( r' \ n ') |
Python string formatting
Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.
In Python, string formatting uses the same syntax as the sprintf function in C
Python string formatting symbols:
symbols |
Description |
%c |
Formatting characters and their ASCII code |
%s |
formatting strings |
%d |
formatting integers |
%u |
Formatting an unsigned integer |
%o |
Formatting an unsigned octal number |
%x |
formatting unsigned hexadecimal numbers |
%x |
Format unsigned hexadecimal number (uppercase) |
%f |
Format floating-point numbers to specify the precision after a decimal point |
%e |
Format floating-point numbers with scientific notation |
%E |
function with%e, format floating-point numbers with scientific notation |
%g |
Shorthand for%f and%e |
%G |
Shorthand for%f and%E |
%p |
Format the address of a variable with hexadecimal number |
Formatting operator Auxiliary directives:
symbols |
function |
* |
Define width or decimal precision |
- |
Used for left justification |
+ |
Show plus sign (+) in front of positive number |
<sp> |
Show spaces in front of positive numbers |
# |
Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' x ' or ' x ') |
0 |
The displayed number is preceded by ' 0 ' instead of the default space |
% |
' percent ' output a single '% ' |
(VAR) |
mapping variables (dictionary parameters) |
M.N. |
M is the minimum total width displayed, and n is the number of digits after the decimal point (if available) |
Python2.6 begins with the addition of a function str.format () that formats the string, which enhances the functionality of string formatting.
Python Triple Quote
Python three quotes allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters. Examples are as follows
1 # !/usr/bin/python3 2 3 """ This is an instance of a multiline string 4 multi-line strings can use tabs 5 TAB (\ t). 6 You can also use line break [\ n]. 7"" "8print (PARA_STR)
The result is:
This is an instance of a multiline string multi-line string can be used with Tab tab ( ). You can also use the line break [ ].
Three quotes allow programmers to escape from the mire of quotes and special strings, keeping a small piece of string in the form of what is known as WYSIWYG.
A typical use case is that when you need a piece of HTML or SQL, a special string escape is cumbersome when you use a string combination.
" " cursor.execute ( 'CREATE TABLE users ( login VARCHAR (8), UID Integer,prid INTEGER)')
The third day of getting started with Python