%s 可以接受一切数据类型
my_message = 'My name is %s, I am %s years old!'% ('Raimond', 34)
print(my_message)
my_message1 = 'My name is %s, I am %d years old!'% ('Raimond', 34)
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
My name is Raimond, I am 34 years old!
My name is Raimond, I am 34 years old!
Process finished with exit code 0
传递浮点数
my_message = 'percent%f'% 99.56431121
print(my_message)
my_message = 'percent%.2f'% 99.56431121
print(my_message)
my_message = 'percent%.1f'% 99.56431121
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
percent99.564311
percent99.56
percent99.6
Process finished with exit code 0
my_message = 'percent%s' % 99.6581651
print(my_message)
my_message = 'percent%.1s' % 99.6581651
print(my_message)
my_message = 'percent%.2s' % 99.6581651
print(my_message)
my_message = 'percent%.3s' % 99.6581651
print(my_message)
my_message = 'percent%.4s' % 99.6581651
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
percent99.6581651
percent9
percent99
percent99.
percent99.6
Process finished with exit code 0
my_message = 'percent%s' % 99.6581651
print(my_message)
my_message = 'percent:%.1s' % 'sadfdsfsdafdsf'
print(my_message)
my_message = 'percent:%.2s' % 'sadfdsfsdafdsf'
print(my_message)
my_message = 'percent:%.3s' % 'sadfdsfsdafdsf'
print(my_message)
my_message = 'percent:%.4s' % 'sadfdsfsdafdsf'
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
percent99.6581651
percent:s
percent:sa
percent:sad
percent:sadf
Process finished with exit code 0
输出百分号
my_message = 'percent%.2f%%' % 99.6581651
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
percent99.66%
Process finished with exit code 0
my_message = 'This is %(name)s, his age is %(age)d'% {'name':'alex', 'age':18}
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
This is alex, his age is 18
Process finished with exit code 0
my_message = 'This is %(name).2f'% {'name':2.4984}
print(my_message)
my_message = 'This is %(name).4f %%'% {"name":2.04984,}
print(my_message)
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
This is 2.50
This is 2.0498 %
Process finished with exit code 0
year = 2019
month = 7
day = 27
print(year, month, day, sep= ‘:’)
print(year, month, day, sep= '-')
C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
2019:7:27
2019-7-27
Process finished with exit code 0