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

python-格式化输出

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

python-格式化输出

目录

1.占位符

2.format格式化函数

3.f 表达式


1.占位符

%d:整数

print("hello,I am %d " %7)

hello,I am 7

%c:字符,Unicode编码对应字符

print("hello,I am %c " %20154)

hello,I am 人

%s:字符串

print("hello,I am %s " %"people")

hello,I am people

%f:小数,默认小数点后六位

print("hello,I am %s " %7)

hello,I am 7.000000

2.format格式化函数

print("{}".format("hello"))

hello

print("{0}  {1}".format("hello","sir"))

hello sir

print("hello {name} ".format(name="sir"))

hello sir

#字典

s={"name1":"candy","name2":"mary" }

print("{name1},{name2}".format(name="sir"))

candy,mary

#列表

l=["hello","sir"]

print("{0[0]},{0[1]"}.format(l))

hello,sir 

#保留小数点后三位

print("{:.3f}".format(1.234567))

1.235

#带符号

print("{:-.3f}".format(1.234567))

-1.235

 3. f 字符串

#将字符串放在{}中

name=‘Bob’

print(f'my name is {name}.')

my name is Bob.

 #可以在{}中放入表达式

apple_price =10

number =20

print(f'I spend {apple_price*number} RMB')

I spend 200 RMB

#保留小数点后两位

apple_price =10.2

number =20.25

print(f'I spend {apple_price*number:.2f} RMB')

I spend 206.55 RMB

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

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

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