【发布时间】:2020-07-07 04:03:23
【问题描述】:
所以我正在尝试解决 PyBites 平台上的一个问题,它要求您执行以下操作:
- 任意字符串
- 将其格式化为小写
- 用 * 符号替换任何元音
- 跟踪改变了多少元音
示例字符串 text = 'Hello World' 应返回以下元组:('h*ll* w*rld', 3),其中 3 表示已更改的总元音。
下面的代码包含一个应该处理所有列出的步骤的函数。我什至使用了赋值,以便我可以使用 .replace() 并输出更改后的字符:
from typing import Tuple
text = 'Hello World'
def strip_vowels(text: str) -> Tuple[str, int]:
vowels = 'aeiou'
count = 0
text = text.lower().splitlines()
for words in text:
for char in words:
for vowel in vowels:
if char == vowel:
count += 1
result = words.replace(char, '*'), count
return result
answer = strip_vowels(text)
print(answer)
我遇到的问题是,虽然我成功检查了字符串中的字符是否为元音,但返回值是关闭的:('hell* w*rld', 3)
我知道 replace() 会在每次迭代时检查元音,但它不会存储所有结果。
关于我应该采取哪些步骤的任何指导? 提前致谢。
【问题讨论】:
-
这能回答你的问题吗? String replace doesn't appear to be working
标签: python-3.x replace substring