- 不可变的变量,如string字符串、tuple元组,所有的元素赋值和切片赋值都是非法的。只有转换成列表才可以修改。
- 设置字符串格式:
#运算符%方法print("My name is %s"%"ABC") #*.format()方法,需要在""中加{},因为方法本质是key映射print("My {} is {}".format("age","32")) print("My {a1} is {a2}".format(a1="age",a2="32")) #f字符串year = 2016event = 'Referendum'print(f'Results of the {year} {event}')
- 字典传递到字符串:
#字典传递到字符串table = { 'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}print('Jack: {0}; Sjoerd: {1}; ' 'Dcab: {2}'.format(table["Jack"],table["Sjoerd"],table["Dcab"]))print('Jack: {0[Jack]}; Sjoerd: {0[Sjoerd]}; ' 'Dcab: {0[Dcab]}'.format(table))table = { 'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))