Python, pythonformat
First, we will introduce the format function in python. Next, we will introduce the simple use of the python. format () function.
--- Restore content start ---
The format function in python is used for string formatting.
Keyword
Print ('{name} today {action }'. format (name = 'chen moumou ', Action = 'shoot video') # Use the keyword grade = {'name': 'chen moumou', 'fenshu ': '59'} print ('{name} electrician tested {fenshu }'. format (** grade) # using keywords, you can use a dictionary to add ** to the dictionary when a value is input.
Pass location
Print ('{1} today {0 }'. format ('shoot video', 'chen moumou ') # print by position (' {0} today {1 }'. format ('chen moumou ', 'shoot video '))
Fill and alignment ^ <> indicates center, left, right, and rear with width.
Print ('{: ^ 14 }'. format ('chen moumou ') print (' {:> 14 }'. format ('chen moumou ') print (' {:< 14 }'. format ('chen moumou ') print (' {: * <14 }'. format ('chen moumou ') print (' {:&> 14 }'. format ('chen moumou ') # Fill and align ^ <> center, left, right, and back with width
Precision and Type f accuracy are often used with f
print('{:.1f}'.format(4.234324525254))print('{:.4f}'.format(4.1))
Hexadecimal conversion. B o d x represents binary, octal, decimal, and hexadecimal respectively.
Print ('{: B }'. format (250) print ('{: o }'. format (250) print ('{: d }'. format (250) print ('{: x }'. format (250) delimiter, which is only applicable to print ('{:,}'. format (100000000) print ('{:,}'. formats (235445.234235 ))
The following describes the format function in python:
Used to format strings
1. Pass by location
Print ('{0} xxxxxxxx {1}'. format ('this is what 0 is going to pass in ', 'this is what 1 wants to pass in '))
2. pass through keywords
print('{0}xxxxxxxx{1}'.format(0='xxx',1='xxx'))
For dictionary
Print ('{keys} xxxxxxxx {values}'. format (** <dictionary Name> ))
3. Act as the thousands separator for numbers (for example, 1,000,000)
print('{:,}'.format(1000000))
Summary
The above is a simple tutorial on using the format () function in python. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!