40、常用字符串格式化有哪几种?

Python的字符串格式化常用的有三种!

  第一种最方便的

  缺点:需一个个的格式化

print('hello %s and %s'%('df','another df'))

  第二种最好用的

  优点:不需要一个个的格式化,可以利用字典的方式,缩短时间

print('hello %(first)s and %(second)s'%{'first':'df' , 'second':'another df'})

  第三种最先进的

    优点:可读性强

print('hello {first} and {second}'.format(first='df',second='another df'))
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/9215546.html