Formatted string output of Python print() function

Time:2022-5-7

By using floating-point number and string to explain the format printing method of ptint() function, we can print the information we need on the console at will in the development process.

file

[read the full text]

The first is the format printing method of the decimal point in the console.

Format the decimal point by%. 3F

Print ("%. 3F"% 3.1415926) # keep 3 decimal places

Print ("%. 2F"% 3.1415926) # keep 2 decimal places

Format the decimal point by {:. 3F}

Print ("{:. 3F}". Format (3.1415926)) # keep 3 decimal places

Print ("{:. 2F}". Format (3.1415926)) # keep 2 decimal places

Ensure the formatting of decimal points through the round() function

Print (round (3.1415926, 3)) # retains 3 decimal places

Next is the string formatting method.

Initialize a and B strings for string formatting

A = 'the result of a is:'
b = 10

Format using%

print(" %s%d " % (a, b))

Use the format () function to format. A and B are formatted in order by default

print("{}{}".format(a,b))

Format with format() function and format with ‘{0} {1}’ placeholder

print("{0}{1}".format(a,b))

Format with format() function and format with ‘{a} {B}’ parameter value placeholder

print("{a}{b}".format(a=a,b=b))

“>” use right alignment

Print ('name: {0:>5} \ ntype: {1:>5}'.format ('official account ',' Python concentration camp '))

Print ('name: {0:<5} \ ntype: {1:<5}'.format ('official account ',' Python concentration camp '))

Format using f ‘{}’, which supports Python 3 Version above 6

Print (the result of f'a is: {a} the result of B is: {B} ')

Join [Python concentration camp] to get more benefits!
file

Pyqt5 GUI & & requests API make a weather query system (get the complete code at the end of the text)!

A beautiful windows CMD command line tool cmder

How to write and save visual data after Excel data analysis!

Excel data processing II: quickly complete the addition and modification of openpyxl data!

Excel data processing 1: skillfully use openpyxl to extract and filter data

Recommended Today

Seata distributed transaction failure notification

1、 Background In our useSeataAs a distributed transaction, sometimes our distributed transaction can not succeed every time, and we need to notify these failed distributed transactions. This article briefly records how to implement notification. 2、 Function realization The email notification is simulated here, but the email is not really sent, but a log is simply […]