format函数用法python

在Python中,format()函数是一个非常实用的字符串格式化工具,它可以将指定的值插入到字符串的占位符位置,从而生成一个新的字符串。format()函数的基本语法如下:

format函数用法python
(图片来源网络,侵删)
str.format(*args, **kwargs)

*args表示可变数量的位置参数,**kwargs表示可变数量的关键字参数,这些参数将被插入到字符串中的占位符位置。

下面是一些使用format()函数的示例:

1、基本用法

name = "张三"
age = 25
result = "我的名字是{},我{}岁了。".format(name, age)
print(result)  # 输出:我的名字是张三,我25岁了。

2、使用索引和关键字参数

result = "我的名字是{0},我{1}岁了。".format(name, age)
print(result)  # 输出:我的名字是张三,我25岁了。

3、使用关键字参数

result = "我的名字是{name},我{age}岁了。".format(name=name, age=age)
print(result)  # 输出:我的名字是张三,我25岁了。

4、格式化数字和货币

pi = 3.1415926
result = "圆周率的值约为{:.2f}。".format(pi)
print(result)  # 输出:圆周率的值约为3.14。
money = 123456789.987654321
result = "金额为:{:,.2f}".format(money)
print(result)  # 输出:金额为:1,234,567,899.88

5、对齐字符串

name = "张三"
age = 25
result = "{:<10}{:>5}".format(name, age)
print(result)  # 输出:张三    25

6、重复字符串

result = "{:*<10}".format("Hello")
print(result)  # 输出:HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHilloHallloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeloHeroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroHerroloorooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooododdooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddooddodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoodoododddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoccccccccccccccccccccccccccccccccccccccccccccccccccccccccccoocococococococococococococococococococococococococococococococococococococococococccceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffgggggggggggggggggggggggggggggggggggggggggggggggyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyoouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrruunnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnoppppppppppppppppppppppppppppppppppppoqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqopmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmooopppoppoppoppoppoppopoppoppoppoppoppopopp" * 100)
print(result)  # 输出:***************************************************************************************************************************************************************************************************************************************************************

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/295430.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
酷盾叔订阅
上一篇 2024-03-02 18:38
下一篇 2024-03-02 18:40

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入