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

format函数用法pythonformat函数用法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)  # 输出:***************************************************************************************************************************************************************************************************************************************************************
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。