总结一些常用的python中String的操作,以后查阅起来方便一些。
- {} + .format() 的形式
print("Floating point {0:.2f}".format(345.6789))
# f代表浮点型, 2代表两位小数
# Floating point 345.67
import math
print("Floating point {0:10.3f}".format(math.pi))
# 10代表宽度,表示左边的空格数加上数字位数和小数点的总宽度(即左边空格为5格)
# Floating point 3.142
print("Sam has {1:d} red balls and {0:d} yellow balls".format(12, 31))
# d代表整型
# Sam has 31 red balls and 12 yellow balls
print("Sam has {red} red balls and {green} yellow balls".format(red=12, green=31))
# Sam has 12 red balls and 31 yellow balls
print("In binary 4 is {0:b}".format(4))
# b 代表二进制类型
# In binary 4 is 100
strip()
方法一处字符串头尾指定的字符(默认为空格或换行符)or字符序列。' '.join(L)
将以字符串构成的列表L中的每个元素以’ '空格相连接
以后会补充正则表达式的内容