3.6 string format operation
The method and format strings are a lecture on a type, function strings are BIF of the python
format string, in accordance with the rules of homonyms, another string to obtain uniform output
1. format () function
Curly braces}, respectively {field, the string passed to the used position parameters
str1 = "{0} love {1}.{2}".format('I','fishC','com')
print(str1)
The key parameter, a, b, c, etc., but must use the index to specify
str1 = "{a} love {b}.{c}".format(a = 'I',b = 'fishC',c = 'com')
print(str1)
#相当于引入了三个变量
Position parameters, such as 0,1,2, or other
may be used in conjunction with positional parameters and keyword parameters, it should be noted that
the position of the keyword parameters before the parameter must
str1 = "{0} love {b}.{c}".format('I',b = 'fishC',c = 'com')
print(str1)
Print braces
str2 = "{{0}}".format("not print")
print(str2) #not print字符串没有被打印,没有字段可以被输出,打印{0},相当于反斜杠转义符
: Indicates the start formatting symbols
str3 = '{0:.1f}{1}'.format(27.678921,'GB') #.1 表示一位小数点,四舍五入. f表示定点数,区别于浮点数,都是打印小数
print(str3) #打印27.7GB
2. The meanings of the symbols string formatting
Enjoy a unique string of symbols:%
when the string encountered%, the remainder will change the meaning of
symbol | effect |
---|---|
%c | Formatting characters and their ASCII code |
%s | Format string |
%d | Integer format |
%The | Formatting unsigned octal |
%x | Unsigned hexadecimal format |
%X | Unsigned hexadecimal format (uppercase) |
%f | Format floating point number, the accuracy after the decimal point can be specified, the default 6 |
%e | Floating-point format in scientific notation |
%E | Action with% e, floating point numbers formatted in scientific notation |
%g | Or using% f% e value determined according to the size |
%G | Action with% g,% f or% E used according to the size of the determined value |
str1 = '%c' % 97
print(str1) #打印a
str1_1 = '%c %c %c' % (97,98,99)
print(str1_1)
str2 = '%s' % 'I love weivid'
print(str2) #将字符串或其他变为字符串
str3 = '%d + %d = %d' % (4, 5, 4+5)
print(str3) #传递参数至左边字符串中
str4 = '%o' % 43
print(str4) #打印53,43用八进制表示为53 = 5*8 + 3
str4_1 = '%x' % 10
print(str4_1) # 打印a
str5 = '%f' % 27.658
print(str5)
3. The operator assist command Format
symbol | effect |
---|---|
m.n | m is the minimum overall width of the display, n is the number of digits after the decimal point |
- | For the left-aligned |
+ | Is displayed in front of the positive plus sign (+) |
# | It is displayed in front octal '0o', display '0x' or '0X' in front of a hexadecimal number |
0 | The figures show the front padding '0' substituted space |
str6 = '%5.1f' % 27.658
print(str6) #整个字符串宽度为5,小数一位
str6_1 = '%.3e' % 27.658
print(str6_1) #科学计数法表示,三位小数
str6_2 = '%10d' % 5
print(str6_2) #总位宽为10
str6_3 = '%-10d' % 5
print(str6_3) #总位宽为10,5在最左边
str6_4 = '%+10d' % 5
print(str6_4) #总位宽为10,显示+5
str7 = '%#o' % 12
print(str7) #打印0o14,表示这个是八进制数14
str7_1 = '%#x' % 108
print(str7_1) #打印0x6c,表示这个是八进制数14
str8 = '%010d' % 5
print(str8)
4. Python escape characters and their meanings
symbol | effect |
---|---|
’ | apostrophe |
" | Double quotes |
\a | Issued a ringing sound system, no use, the system requires a combination of |
\b | Backspace |
\n | Newline |
\t | Horizontal tab (TAB) |
\ v | Vertical tab |
\r | Carriage return |
\f | Page breaks |
\The | Character octal number represented |
\x | Character hexadecimal number represented |
\0 | It represents a null character |
\ | Backslash |