Zero-based entry learning Python (14)-format string

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

Insert picture description here
Insert picture description here
Insert picture description here
attention: When combining positional parameters and keyword parameters, the positional parameters must be before the keyword parameters, otherwise an error will be reported
Insert picture description here

How to print out {}? ? ?

Insert picture description here
Guess what the following code will print?

>>> '{0:.1f}{1}'.format(27.658,'GB')

Insert picture description here

  • 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%

  • When% encounters a string,% becomes an operator exclusive to the string

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
  • %c: formatting characters and their ASCII codes

Insert picture description here
Insert picture description here

  • %s: format string

Insert picture description here

  • %d: formatted integer

Insert picture description here

  • %o: format unsigned octal number (decimal → octal)

Insert picture description here

  • %x: format unsigned hexadecimal number

  • %X: Format unsigned hexadecimal number (uppercase)

Insert picture description here

  • %f: Format floating-point numbers, you can specify the precision after the decimal point

Insert picture description here

  • %e: Format floating point numbers in scientific notation

  • %E: Same function as %e, format floating point numbers in scientific notation

Insert picture description here

  • %g: Decide to use %f or %e according to the value

  • %G: Same function as %g, use %f or %E according to the value

Insert picture description here

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
  • mn: m shows the minimum total width (occupied), n is the number of digits after the decimal point

Insert picture description here
Insert picture description here

  • -: Result is left aligned

Insert picture description here

  • +: Display plus sign in front of positive numbers

Insert picture description here

  • #: Display '0o' in front of the octal number, and display '0x' or '0X in front of the hexadecimal number

Insert picture description here
Insert picture description here

  • 0: Fill '0' in front of the displayed number instead of spaces

Insert picture description here

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)

Insert picture description here
4. Write a hexadecimal conversion program, the program demonstration is as follows (hint, the decimal conversion binary can use bin() this BIF):
Insert picture description here
print directly, the num inside will be treated as a string

print("十进制 -> 十六进制 :num -> '%X' % num")

Insert picture description here
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))

Insert picture description here
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

Insert picture description here

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

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/113572610