本帖最后由 jackz007 于 2022-9-21 19:40 编辑 S = input("请输入字符串:")
i , T = 0 , ''
while i < len(S) - 1:
if S[i] != S[i + 1] and S[i] . lower() == S[i + 1] . lower():
i += 1
else:
T = T + S[i]
i += 1
if i == len(S) - 1:
T = T + S[i]
print(T)
运行实况:D:\[00.Exerciese.2022]\Python>python x.py
请输入字符串:ABBCCDDEEFFGG
ABBCCDDEEFFGG
D:\[00.Exerciese.2022]\Python>python x.py
请输入字符串:ABbcCDdeE
A
D:\[00.Exerciese.2022]\Python>python x.py
请输入字符串:ABbcCDdeEF
AF
D:\[00.Exerciese.2022]\Python>
用列表的版本S = input("请输入字符串:")
a , i = [] , 0
while i < len(S) - 1:
if S[i] != S[i + 1] and S[i] . lower() == S[i + 1] . lower():
i += 1
else:
a . append(S[i])
i += 1
if i == len(S) - 1:
a . append(S[i])
print('' . join(a))
|