python-2 格式化输出

格式化输出
在这里插入图片描述

age = 18
weight = 70.3
name = "jc"
stu_id = 1
stu_id1 = 1000

# 1.今年我的年龄是x岁 -- %d
print('今年我的年龄是%d岁' %age)

# 2.My name is x -- %s
print('我的名字是%s' %name)

# 3.我的体重x公斤 -- %f
print('我的体重%f公斤' %weight)  # 默认保留6位小数
print('我的体重%.2f公斤' %weight)  # 保留两位

# 4.我的学号是x -- %d
print('我的学号是%d' %stu_id)
# 4.1我的学号是001
print('我的学号是%03d' %stu_id) # 表示输出的证书显示位数,不足的以0补全
print('我的学号是%d' %stu_id1) # 超出的原样输出

# 我的名字是x,年龄是x
print('我的名字是%s,年龄是%d'%(name,age))
print('我的名字是%s,年龄是%d'%(name,age+1)) # 有点参数的感觉

#我的名字是x,年龄是x,学号是x
print('我的名字是%s,年龄是%d,学号是%04d'%(name,age,stu_id))

# 我的名字是x,今年x岁了,体重x公斤 %s的特殊用法
print('我的名字是%s,今年%s岁了,体重%s公斤'%(name,age,weight))

f ‘{ }’ 格式化字符串 (3.6版本新增)

age = 18
name = 'tom'
# 我的名字是x,今年x岁了
print('我的名字是%s,今年%s岁了'%(name,age))

# 语法 f'{表达式}'
print(f'我的名字是{name},今年{age+1}岁了')

猜你喜欢

转载自blog.csdn.net/fly_ship/article/details/108049195
今日推荐