What is a format string
Formatting a string is to output a string according to a uniform specification. If the specification is not uniform, it is likely to cause misunderstanding
format() method
-
acceptLocation parameterwithKeyword parameterTwo parameters
-
Both are passed to onereplacementField, and the replacement field is represented by {} in the string
attention: When combining positional parameters and keyword parameters, the positional parameters must be before the keyword parameters, otherwise an error will be reported
How to print out {}? ? ?
Guess what the following code will print?
>>> '{0:.1f}{1}'.format(27.658,'GB')
-
In the replacement field,colon: Shows the beginning of the formatting symbol, .1 means rounding, with one decimal point reserved, f means to print a fixed-point number (fixed-point numbers and floating-point numbers are similar, that is, print a decimal)
Formatting operator%
symbol | meaning |
---|---|
%c | Formatting characters and their ASCII codes |
%s | Format string |
%d | Format integer |
%The | Format an unsigned octal number |
%x | Format unsigned hexadecimal number |
%X | Format an unsigned hexadecimal number (uppercase) |
%f | Format floating point numbers, you can specify the precision after the decimal point |
%e | Format floating point numbers in scientific notation |
%E | Same function as %e, format floating point number in scientific notation |
%g | Decide to use %f or %e according to the value |
%G | Same effect as %g, use %f or %E according to the value |
-
%e: Format floating point numbers in scientific notation
-
%E: Same function as %e, format floating point numbers in scientific notation
-
%g: Decide to use %f or %e according to the value
-
%G: Same function as %g, use %f or %E according to the value
Formatting auxiliary commands
symbol | meaning |
---|---|
m.n | m shows the minimum total width, n is the number of digits after the decimal point |
- | Result left aligned |
+ | Show plus sign in front of positive numbers |
# | Show '0o' in front of octal number, show '0x' or '0X' in front of hexadecimal number |
0 | Fill the displayed number with '0' instead of spaces |
-
#: Display '0o' in front of the octal number, and display '0x' or '0X in front of the hexadecimal number
Python escape characters and their meaning
symbol | Description |
---|---|
\’ | apostrophe |
\" | Double quotes |
\a | Ring the system |
\b | Backspace |
\n | Newline |
\t | Horizontal tab (TAB) |
\ v | Vertical tab |
\r | Carriage return |
\f | Form feed |
\The | Characters represented by octal numbers |
\x | Characters represented by hexadecimal numbers |
\0 | Represents a null character |
\\ | Backslash |
Task
0. What will be printed in the following line of code?
>>> "{
{1}}".format("不打印", "打印")
{1}, because {1} has been explained
1. In the following code, what are the parameters a, b, and c?
>>> "{a} love {b}.{c}".format(a="I", b="FishC", c="com")
'I love FishC.com'
Keyword parameter
2. In the following code, what are the parameters {0}, {1}, {2}?
>>> "{0} love {1}.{2}".format("I", "FishC", "com")
'I love FishC.com'
Position parameter
3. If you want to display Pi = 3.14, how should you fill in the string before format?
''.format('Pi = ', 3.1415)
'{0}{1:.2f}'.format('Pi = ', 3.1415)
4. Write a hexadecimal conversion program, the program demonstration is as follows (hint, the decimal conversion binary can use bin() this BIF):
print directly, the num inside will be treated as a string
print("十进制 -> 十六进制 :num -> '%X' % num")
Separately print strings and numbers, and connect with end="". There are a lot of prints like this. Another thing that can be improved is to add "0x" and "0o" before hexadecimal and octal.
while 1:
num = input("请输入一个整数(输入Q结束程序):")
if num != 'Q':
num = int(num)
print("十进制 -> 十六进制 :",end = "")
print( num, end = '')
print('->',end = '')
print('%X' % num)
print("十进制 -> 八进制 :",end = "")
print( num, end = '')
print('->',end = '')
print('%o' % num)
print("十进制 -> 二进制 :",end = "")
print( num, end = '')
print('->',end = '')
print(bin(num))
Take a look at the code of the little turtle
q = True
while q:
num = input('请输入一个整数(输入Q结束程序):')
if num != 'Q':
num = int(num)
print('十进制 -> 十六进制 : %d -> 0x%x' % (num, num))
print('十进制 -> 八进制 : %d -> 0o%o' % (num, num))
print('十进制 -> 二进制 : %d -> ' % num, bin(num))
else:
q = False
Use # instead of 0o, 0x
q = True
while q:
num = input('请输入一个整数(输入Q结束程序):')
if num != 'Q':
num = int(num)
print('十进制 -> 十六进制 : %d -> %#x' % (num, num))
print('十进制 -> 八进制 : %d -> %#o' % (num, num))
print('十进制 -> 二进制 : %d -> ' % num, bin(num))
else:
q = False