table of Contents
1.print syntax parameters
print([object,...][,seq=' '][,end='\n'][,file=sys.stdout])
seq delimiter, the default is a single space
end A string added at the end of the printed text, the default is the newline character'\n'
file specifies the file to which the text will be sent, if it is not passed, the default is sys.stdout
x = 'spam'
y = '99'
z = ['eggs']
print(x,y,z,sep='....')
#spam....99....['eggs']
print(x,y,z,sep='...',file=open('data.txt','w'))
2.print formatted output
(1) Python string formatting symbols:
# %c formatting characters and their ASCII codes
# %s format string
# %d format integer
# %u format unsigned integer
# %o format unsigned octal number
# %x format unsigned hexadecimal number
# %X format unsigned hexadecimal number (uppercase)
# %f format floating point numbers, you can specify the precision after the decimal point
# %e Format floating point numbers in scientific notation
# %E has the same function as %e, and uses scientific notation to format floating-point numbers
# %g Shorthand for %f and %e
# %G Shorthand for %f and %E
# %p format the address of the variable with a hexadecimal number
(2) Formatting operator auxiliary instructions:
* #Define width or decimal point precision
-#Used for left alignment
+ #Display the plus sign (+) in front of the positive number
<sp> #Display a space in front of a positive number
# #Display zero ('0') in front of the octal number, and display '0x' or '0X' in front of the hexadecimal number (depending on whether you use'x' or'X')
0 #The displayed number is filled with '0' instead of the default space
% #'%%' output a single'%'
(var) #Mapping variable (dictionary parameter)
mn #m is the minimum total width of the display, n is the number of digits after the decimal point (if available)
(3) Formatted output hexadecimal, decimal, octal integer
#%x --- hex hexadecimal#%d --- dec decimal#%o --- oct octal
3. Examples
# 1. Print string
print ("His name is %s"%("Aviad")); ##结果:His name is Aviad
str1 = "The value is:"
number1 = 11
print("%s %d" % (str1, number1)) # 输出"The value is: 11"
# 2. Print integer
print ("He is %d years old"%(25))
# 3. Print floating point numbers
print ("His height is %f m"%(1.83))
# 4. Print floating-point numbers (specify the number of decimal places to be retained)
print ("His height is %.2f m"%(1.83))
# 5. Specify the width of the placeholder
print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))
# 6. Specify the width of the placeholder (left-justified)
print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))
# 7. Specify a placeholder (can only use 0 as a placeholder?)
print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))
# 8. Scientific Notation
format(0.0015,'.2e')
# 9. Output the specified number of digits
import math;
PI = 3.1415926
print("PI = %10.3f"% math.pi) # output PI = 3.142
print("PI = %-10.3f"% math.pi) # output PI = 3.142
# 10. Output the specified length
print("%.3s" %("abcde")) #输出abc
print("%.*s" %(4,"abcde")) #输出abcd
print("%10.3s" %("abcde")) #output abc (the total length is 10, the character length is not enough to fill in the space in front)
print("%3d %0.2f"%(5,1276.2851)) #output 5 1276.28
# 11. Print multiple lines
print(""" your content""") or print('''your content''')
# 12. Print plain text without using escape characters: print(r'content') or print(R'content')
print(r'abc\n') #Directly print the string abc\n