在python中怎样格式化字符串?

2026-03-31 18:01:21

1、首先打开python 的编辑器新建一个文件data.py

在python中怎样格式化字符串?

2、写上自己的注释内容!

在python中怎样格式化字符串?

3、然后新建一个变量test。

test="my name is {name} and I am {year} old"

在python中怎样格式化字符串?

4、新建完变量之后,通format来实现对字符串的额格式化!

test.format(name="luo",year=26)

在python中怎样格式化字符串?

5、然后将结果打印出来!

print(test.format(name="luo",year=26))

在python中怎样格式化字符串?

6、在窗口中选择"run"->"run"

在python中怎样格式化字符串?

7、最后运行这个程序!

在python中怎样格式化字符串?

猜你喜欢