假设有一段英文,其中有单词中间的字母“i”误写为“I”,请编写程序进行纠正。(使用isalpha())
输入内容为: "If the implementatIon is hard to explain, it's a bad idea."
# isalpha() ,检测字符串是否只由字母组成,如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。
s="If the implementatIon is hard to explain, it's a bad idea."
# 输出字符串
outs=""
# 遍历元字符串每个元素
for i in s:
if not i.isalpha() and i=="|":
# 不是英文字符同时遍历出的字符串等于"|"
outs+="i"
else:
# 是英文字符或者不是英文字符,字符串不是"|"的原样输出
outs += i
print(outs)
"I"+str[1:].replace('|','i')
str = "If the |mplementatIon |s hard to explain, |t's a bad idea."
str_list = []
for i in str:
if i.isalpha() == False:
if i == '|':
i = 'i'
str_list.append(i)
result = ''.join(str_list)
print(result)