先来看看一些基础的函数
a = "AbCdeFG"
#这两个函数负责把字符全部变成大写和小写
print( a.lower() + " " + a.upper())
#split函数将字符串按特定的符号切开
print( "a,b,c".split(",") )
#cout函数返回子串在主串的个数
print("an apple a day".count("a") )
#replace函数替换主串的字符
print("python".replace("n" , "123.com"))
#center函数用于居中
print("pyrhon".center( 20 , "=" ))
#strip函数负责去除字符串左右两边的符号
print( "=python=".strip(" =np") )
#join在变量除最后一个元素外,在每个元素后增加一个字符串
print( ",".join("12345") )
运行结果如下
接着我们再看format函数中的字符使用
print("{0}:计算机{1}的cup占用率为{2:.2f}%".format("2020-10-12" , "C" , 10.0 ) )
print("{:>20.2f}".format(1.21))
print("{:,.2f}".format(12345))