gpt4 book ai didi

python - 考虑到python中的一些条件,如何在字符串中插入一个字符

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:48 26 4
gpt4 key购买 nike

假设我会要求用户输入一个字符串。如何在每个以“a”、“e”或“o”结尾的单词末尾添加一个字符?

例如,来自

f = "O, awesome people, help me if ya will"

我会

f = "Oh, awesomeh peopleh, help meh if yah will"

最佳答案

使用re.sub

re.sub(r'(?i)([aeo])\b', r'\1h', s)
  • (?i) 有助于进行不区分大小写的匹配。
  • ([aeo]) 仅当 a、e、o 后跟单词边界时才捕获它。

示例:

>>> import re
>>> f = "O, awesome people, help me if ya will"
>>> re.sub(r'(?i)([aeo])\b', r'\1h', f)
'Oh, awesomeh peopleh, help meh if yah will'

关于python - 考虑到python中的一些条件,如何在字符串中插入一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com