网站实现,北京seo优化费用,六种常见网络营销方法,深圳哪家网站建设公司好173.jpg概述在Python3中#xff0c;字符串格式化操作通过format()方法或者fstring实现。而相比于老版的字符串格式化方式#xff0c;format()方法拥有更多的功能#xff0c;操作起来更加方便#xff0c;可读性也更强。该函数将字符串当成一个模板#xff0c;通过传入的参数…173.jpg概述在Python3中字符串格式化操作通过format()方法或者fstring实现。而相比于老版的字符串格式化方式format()方法拥有更多的功能操作起来更加方便可读性也更强。该函数将字符串当成一个模板通过传入的参数进行格式化并且使用大括号{}作为特殊字符代替%。位置设定默认位置不指定格式化位置按照默认顺序格式化S I {} {}, and I\am learning.format(like, Python)print(S)示例结果I like Python, and Iam learning设置位置设置数字顺序指定格式化的位置S I {0} {1}, and I\am learning.format(like, Python)print(S)# 打乱顺序S I {1} {0} {1}, and I\am learning.format(like, Python)print(S)示例结果I like Python, and Iam learningI Python like Python, and Iam learning设置关键字设置关键字指定格式化的内容S I {l} {p}, and I\am learning.format(pPython, llike)print(S)S I {p} {l}, and I\am learning.format(pPython, llike)print(S)示例结果I like Python, and Iam learningI Python like, and Iam learning参数传递我们可以传入各种类型参数格式化字符串即不限于字符串变量或数字等。元组传参利用元组传参传参形式 *tuple# 定义一个元组T like, Python# 不指定顺序S I {} {}, and I\am learning.format(*T)print(S)# 指定顺序S I {0} {1}, and I\am learning.format(*T)print(S)示例结果I like Python, and Iam learningI like Python, and Iam learning字典传参# 定义一个字典D {l:like, p:Python}# 指定键确定顺序S I {l} {p}, and I\am learning.format(**D)print(S)示例结果I like Python, and Iam learning列表传参# 定义一个列表L0 [like, Python]L1 [ , Lerning]# []前的0、1用于指定传入的列表顺序S I {0[0]} {1[1]}, and I\am learning.format(L0, L1)print(S)示例结果I like Lerning, and Iam learning