python将字符串中的元音字母变为大写
以下是一个示例代码:
def uppercase_vowels(string):
vowels = ['a', 'e', 'i', 'o', 'u']
new_string = ''
for char in string:
if char.lower() in vowels:
new_string += char.upper()
else:
new_string += char
return new_string
# 示例用法
print(uppercase_vowels('hello world'))
# 输出:hEllO wOrld
该函数的作用是将字符串中的元音字母变为大写,其他字母不变。在函数中,我们首先定义了一个元音字母列表,然后遍历字符串中的每个字符。如果字符是元音字母,则将其转换为大写,并添加到新字符串中;否则直接添加到新字符串中。最后返回新字符串即可。
原文地址: https://www.cveoy.top/t/topic/ehRn 著作权归作者所有。请勿转载和采集!