在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) # 输出:***************************************************************************************************************************************************************************************************************************************************************
最新评论
本站CDN与莫名CDN同款、亚太CDN、速度还不错,值得推荐。
感谢推荐我们公司产品、有什么活动会第一时间公布!
我在用这类站群服务器、还可以. 用很多年了。