# Understand basic formatting name=" Xiao Ming Wang " print(" My name is "+name) # Output results : My name is Wang Xiaoming # String formatting %s
print(" My name is %s"%name) # Output results : My name is Wang Xiaoming print('----------------------') age=18
#print(" My age is "+age) report errors + You can only connect strings # age2=19 # print(age+age2) If both are integers + It means addition
Output results :37 # integer ( integer ) format %d print(' My age is %d year '%age) # Output results : My age is 18 year
print('-----------------------') weight=50.5 # Floating point format %f #
print(" My weight is %f kg . "%weight) # output : My weight is 50.500000 kg . , It doesn't accord with our ideal result
print(" My weight is %.1f kg . "%weight) # %.xf==>x How many digits are reserved after the decimal point Output results : My weight is 50.5 kg .
print('-----------------------') stu_id=1 stu_id2=1000 # Advanced shaping and formatting %0xd
print(" My student number is :%d"%stu_id) print(" My student number is :%03d"%stu_id) #
%0xd==>x Number of digits for output , Display digits of the output integer , not enough 0 completion print(" My student number is :%03d"%stu_id2) # If it exceeds the current digit, it will be output as is """
The output result is : My student number is :1 My student number is :001 My student number is :1000 """ print('----------------------') #
Format both integer and string print(" My name is :%s, My age is :%d year "%(name,age)) # Format multiple at once ==>%( value , value ) Fill in in order ,
separate print(" My name is :%s, next year %d year "%(name,age+1)) ''' Output results : My name is : Xiao Ming Wang , My age is :18 year
My name is : Xiao Ming Wang , next year 19 year ''' print('----------------------')
print(" My name is :%s, I this year %d year , My weight is :%.1f kg . , My student number is :%.03d"%(name,age,weight,stu_id)) #
Output results : My name is : Xiao Ming Wang , I this year 18 year , My weight is :50.5 kg . , My student number is :001 # expand %s format string , In fact, it is equivalent to outputting the original data
print(" My name is :%s, I this year %s year , My weight is :%s kg . , My student number is :%s"%(name,age,weight,stu_id)) #
Output results : My name is : Xiao Ming Wang , I this year 18 year , My weight is :50.5 kg . , My student number is :1 # Another form of formatted string :f'{ expression }'
f- The format string is 3.6 New formatting method in
print(f" My name is :{name}, I this year {age} year , My weight is :{weight} kg . , My student number is :{stu_id}") #
Output results : My name is : Xiao Ming Wang , I this year 18 year , My weight is :50.5 kg . , My student number is :1

Technology