字符串对象的函数
1.string.capitalize( )
将string对象的首字母大写
print('hello'.capitalize())
2.string.center( width ,‘自定的填重符号,默认空格’)
string.center( width ,‘自定的填重符号,默认空格’)
print('ziaoming'.center(30,'-'))
string.ljust(width) 返回 string 占 width 宽度,左对齐
string.rjust(width) 返回 string 占 width 宽度,右对齐
如 99 乘法表,需要左对齐
for i in range(1,10):
for j in range(1,i+1):
print( f"{j}x{i}={j*i}".ljust(8) ,end='')
print()
3.string.count(str, beg=0, end=len(string))
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
# 统计 哄 出现的次数
print( str3.count('哄') )
# 从第三个位置到倒数第三个位置区间内,找有多少个 哄
print( str3.count('哄' ,2,-2) )
4.decode()编码 encode()解码
# 假设一个情景,你是一个特务,你需要将你的情报加密一下
mibao = '今晚回家吃3个鸡腿'
print(mibao)
jiami = mibao.encode('utf-8')
print(jiami)
# 接头人接到了 jiami ,他回去解密
jiemi1 = jiami.decode('gbk',errors='ignore')
print(jiemi1)
jiemi2 = jiami.decode('utf-8')
print