python的字符串格式化

%d可以实现对数字的格式化

num = int(input('how old are you:'))
print('my age is %d'%num)

%f可以实现对小数的格式化

print('prince is %f'%4.99)

输出的是prince is 4.990000,如果想要保留2位小数,在f前面加数字2即可

%s用来替换一段字符串

name='arashi'
print('%s is a great gruop'%name)

猜你喜欢

转载自www.cnblogs.com/arashi/p/9103774.html