Detailed explanation of Python print output function

Hello, everyone, I am classmate Yu. I'm here to update the article again, I hope it can help everyone!

basic usage

output variable

Let's talk about output variables first. printFunctions have a huge advantage. As long as they are variables, they can be completely output, regardless of type.
Unlike C language, the type must be converted

a=10 #整型变量
print(a)
b=10.12 #浮点型变量
print(b)
c='CSDN' #字符串变量
print(c)
d=('1', 2, 'hello') #元组变量
print(d)
e=['1', 2, 'hello'] #列表变量
print(e)
f={
    
    'hello':1, 'world':2} #字典变量
print(f)

operation result:
test 1

You can also output multiple variables at once, ,separated by commas (commas must be used)

a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
    
    'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f)

insert image description here

PythonThe default spacer is a space, we can change the spacer to other characters
such as:

a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
    
    'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f,sep="\n") #'\n'表示换行符

test3
Change the spacer to a dot.

a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
    
    'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f,sep=".") #'\n'表示换行符

test4

format string

directly on the form

Format illustrate
%s string format
%c Characters and their ASCII codes
%d Decimal有符号整数
%u Decimal无符号整数
%f real number (with decimal point symbol)
%e Real numbers (scientific notation)

pending decimal

Note, this is a very important knowledge point, you must remember it!

ch=2.7182818
print('ch=%.*f' %(3,ch)) #保留3位小数

test5
Let's see if Pythonit will be rounded when retaining decimals

ch=1.1999
print('ch=%.*f' %(1,ch)) #保留1位小数

test6
Answer:
It is very convenient to be rounded

Another way is to use formatthe function

ch=3.1415926
print('ch={:.3f}'.format(ch))

more2


Positive output

Output +numbers with a positive sign ( ) (default 保留6位小数):

ch=2.7182818
print("%+f" %ch)

more1


Common format string usage

1. Output integer%d
time=13
print("It is %d o'clock." %time)

test7

2, the output string is used%s
ch="programming"
print("I love %s ." %ch)

test8

3. Output decimals%f
avg=95.5
print("The average score of our class is %f" %avg)

test9

We found that %fwhen using the format string, Pythonby default 保留6位小数, if you want to keep other digits, you can write like this

avg=95.5
print("The average score of our class is %.*f" %(1,avg))

insert image description here

Terminators and separators

terminator

printThe default terminator of a function is a newline ( \n)

for i in range(0,5):
    print("%d" %i) #print(i)

test10

We can end=''control the terminator with a function
For example, change the terminator to a space

for i in range(0,5):
    print("%d" %i,end=" ") #结束符为空格

test11

You can also use more characters to replace the terminator

for i in range(0,5):
    print("%d" %i,end="~") #将结束符设为 ~

test12

spacer

printThe default delimiter of the function is a space ( )

a=10
b=20
print(a,b)

test12

We can sep=''control the spacer with the function

a=10
b=20
print(a,b,sep="\n")

test 13
sep=''Functions and end=''function brackets can be filled任意字符


Guess you like

Origin blog.csdn.net/weixin_45122104/article/details/125958289