# Format string: Prefixes the string with f or uses the value of the variable name in the form of {variable name}
year = 2020
event = 'Referendum'
value = f'Results of the {year} {event}'
print(f'Results of the {year} {event} \n', value)

# :.3f indicates to keep the preceding variable values/literals 3 decimal places, 3d, 3 is to make the cover section the minimum character width, which is useful for column alignment
print(f'The value of pi is approximately {3.1415926:3.f}. ')
print(f'The value of pi is approximately {31415926: 3d}. ')
print(F'The value of pi is approximately {3.1415926: 3}. ')

# str.format Formats a string
# Index form
print('The value of pi is approximately {0}.'.format('ha ha'))

# Keyword form
print('The value of pi is approximately {name}.'.format(name='jobi'))


# % Format string (old) %5.3f Total length 5 reserved 3 decimal places
print('The value of PI is approximately %5.3 F.' % 3.141592678)

The # repr() STR () function converts the object to a string

f1 = 22.3
print(type(repr(f1)))

# str.rjust(number of Spaces) fills the left side of the string with Spaces
for x in range(1.11) :print(repr(x), str(x * x).rjust(4), end='\n')

# str.ljust(number of Spaces) fills the right side of the string with Spaces
for x in range(1.11) :print(repr(x), str(x * x).ljust(4), end='\n')

# str.center(total number of Spaces) fills the middle of the string with the total number of Spaces /2 at the end of the string
for x in range(1.11) :print(repr(x), str(x * x).center(4), end='\n')      # 10, 100

The number of zeros to the left of str.zfill is the length after filling - the length before filling
print('12'.zfill(3))    # 012
Copy the code

Content from the official website :docs.python.org/zh-cn/3.7/t…