Python string three formatted output

String formatted output is a very important basic grammar for python. Today, I will make a simple summary of the three formatted outputs, I hope it will be helpful to everyone.

**Formatted output: The content is output according to certain format requirements. **

1. Use placeholder% output

Before the python2.6 version, the output format of the C language was used for the% format string.

Instructions for use:

print("format string"% variable)

Use tuple format for more than 2 variables:

print("format string"% (variable 1, variable 2))
Use% placeholders to indicate the position of the variable in the string.
The value passed in should correspond to the variable of the% placeholder one by one.
Among them, %s represents a string, %d represents an integer, and %f represents a decimal (the default is to retain 6 decimal places, and %.2f retains two decimal places). When there is a formatting flag, %% needs to be used to represent a percent sign.

name='xiaoming'
age=12print("My name is %s,My age is %d"%(name,age))
# Output: My name is xiaoming,My age is 12

2. format format

format is a new method for formatting strings in python2.6. Compared with the% formatting method, it has the following advantages:

Instructions for use:

print("...{index}, ... , {index}, ...".format(Value 1,Value 2))
# index{}Empty, the default value is in order
print("...{key1}, ... , {key2}, ...".format(key1=value,key2=value))
name='xiaoming'
age=12print('My name is {}, My age is {}'.format(name,age))print('My name is {0}, My age is {1}'.format(name,age))print('My name is {name}, My age is {age}'.format(name='xiaoming',age=12))
# Output: My name is xiaoming,My age is 12

formatadvanced

1. Fill alignment

# Get the value first,Then set the padding format after the colon:{index:[Fill character][Alignment][width]}
# *<20 : Left-justified, a total of 20 characters, not enough*Number fill
print('{0:*<20}'.format('hellopython'))
# *>20 : Right justified, 20 characters in total, not enough*Number fill
print('{0:*>20}'.format('hellopython'))
# *^20 : Center display, a total of 20 characters, not enough*Number fill
print('{0:*^20}'.format('hellopython'))
Output:
hellopython******************hellopython
****hellopython*****

2. Digit and base conversion

# Keep 2 significant figures
print("{:.2f}".format(3.1415926))
# Convert to binary
print('{0:b}'.format(16))
# Convert to octal
print('{0:o}'.format(10))
# Convert to hexadecimal
print('{0:x}'.format(15)) 
Output
3.141000012
f

f-string format

F-strings was introduced in Python 3.6, which is not only easier to use than str.format, but also more efficient.

Instructions for use

f-string is the string with "f" before it, {} directly uses variables, expressions, etc.

name='xiaoming'
age=12
#{} Use variables directly in
print(f'My name is {name},My age is {age}')
#{} Run expression
print(f'{1+2+3}')
# Call Python built-in functions
print(f'{name.upper()}')
# Use lambda anonymous functions: you can do complex numerical calculations
fun = lambda x : x+1print(f'{fun(age)}')
# Output
My name is xiaoming,My age is 126
XIAOMING
13

Recommended Posts

Python string three formatted output
Python string
Python string manipulation
Str string in Python
How Python converts string case
How does Python output integers
Python interview questions: string concatenation
Python output mathematical symbols example
XTU programming Python training three
Python interview questions collection (three)
The premise of Python string pooling
Python classic programming questions: string replacement
Python realizes console output color font