在Python中,format()
函数是一个非常实用的字符串格式化工具,它可以将指定的值插入到字符串的占位符位置,从而生成一个新的字符串。format()
函数的基本语法如下:
(图片来源网络,侵删)
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
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复