栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python中format的用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中format的用法

Python中format的用法

#format的用法

#d表示整数数据类型
x = 4
y = 5
z = x + y
print("Output #2: Four plus five equals {0:d}.".format(z))

'''
{0:d}表示我们需要输出的值,0表示format中第一个未知数,d则表示数据形式为整数
'''

'''
Output #2: Four plus five equals 9.
'''


#f表示浮点数数据类型
from math import exp, log, sqrt
print("Output #11: {0:.4f}".format(exp(3)))
print("Output #12: {0:.2f}".format(log(4)))
print("Output #13: {0:.1f}".format(sqrt(81)))

'''
Output #11: 20.0855
Output #12: 1.39
Output #13: 9.0
'''

#s表示字符串数据类型
string1 = "This is a "
string2 = "short string."
sentence = string1 + string2
print("Output #18: {0:s}".format(sentence))
print("Output #19: {0:s} {1:s}{2:s}".format("She is", "very "*4, "beautiful."))
m = len(sentence)
print("Output #20: {0:d}".format(m))

'''
Output #18: This is a short string.
Output #19: She is very very very very beautiful.
Output #20: 23
'''


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号