Python 字符串 修饰 变形 方法

str = '''Whatever pleasure Myra may have shown at the 
commencement of this speech gave way to a mutinous frown 
as its later purport penetrated her mind. Had she not had his 
explicit promise that she should not be directly involved in 
the handling of these illicit 
'''
# 全文:查找is在字符串当中出现的次数
# 第一行:查找Myra第一个出现的位置
# 第二行:将第二行分割成'commencement of this speech gave ',
# 'way',' to a mutinous frown '
# 第三行:将mind替换成branie,将p和o 替换成a和z,指定translates方法
# 第四行:将promise提取出来,将第四行反向打印输出
# 第五行:判断字符串是否都是字母组成,判断每个单词首字母都是大写

print(str.count('is'))          #查找is在字符串当中出现的次数
#全文
print(str.find('Myra'))         #查找Myra第一个出现的位置

result=str.splitlines()         #将第二行分割
print(result)
line2=result[1]
print(line2)
str1=line2
result1= line2.partition('way')
print(result1)

print(str.replace('mind', 'branie', 1))       #将mind替换成branie
line3=result[2]
print(line3)
intab = 'po'          #将p和o 替换成a和z
outtab = 'az'
line3_trantab = line3.maketrans(intab, outtab)     #用translates方法
test_line3 = result[2]
result2 = test_line3.translate(line3_trantab)
print(result2)

print( result)
line4=result[3]
print( line4)
promise = result[3]
print(promise[8:16])        #将promise提取出来
print(line4[::-1])          #反向打印输出

line5=result[4]
print(line5)
line5 = ''
bool_res1 = line5.isalpha()         #判断字符串是否都是字母组成
print(bool_res1)
line5 = ''
bool_res5 = line5.istitle()         #判断每个单词首字母都是大写
print(bool_res5)

猜你喜欢

转载自blog.csdn.net/weixin_44374605/article/details/85856785