Python - 用特定字符K 替换字符串中的元音
给定一个字符串,用字符K 替换所有元音。
Input : test_str = “Geeks for Geeks”; K=’#’
Output : “G##ks f#r G##ks”
Explanation : All the vowels in test_str are replaced by a give particular character.
Input : test_list = “GFG”; K=”$”
Output : GFG
Explanation : As test_str contained no vowel, so the same string is returned.
方法#1:使用循环+replace()
最初创建字符串元音,然后遍历给定的test_str , 在test_str中检测元音时,使用replace()方法将元音替换为K。
Python3
# Function to Replace each vowel in
# the string with a specified character
def replaceVowelsWithK(test_str, K):
# string of vowels
vowels = 'AEIOUaeiou'
# iterating to check vowels in string
for ele in vowels:
# replacing vowel with the specified character
test_str = test_str.replace(ele, K)
return test_str
# Driver Code
# input string
input_str = "Geeks for Geeks"
# specified character
K = "#"
# printing input
print("Given Sting:", input_str)
print("Given Specified Character:", K)
# printing output
print("After replacing vowels with the specified character:",
replaceVowelsWithK(input_str, K))
Python3
# Function to Replace each vowel
# in the string with a specified character
def replaceVowelsWithK(test_str, K):
# creating list of vowels
vowels_list = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
# creating empty list
new_string = []
# converting the given string to list
string_list = list(test_str)
# running 1st iteration for
# comparing all the
# characters of string with
# the vowel characters
for char in string_list:
# running 2nd iteration for
# comparing all the characters
# of vowels with the string character
for char2 in vowels_list:
# comparing string character
# and vowel character
if char == char2:
# if condition is true then adding
# the specific character entered
# by the user in the new list
new_string.append(K)
break
# else adding the character
else:
new_string.append(char)
# return the converted list into string
return(''.join(new_string))
# Driver Code
# input string
input_str = "Geeks for Geeks"
# specified character
K = "#"
# printing input
print("Given Sting:", input_str)
print("Given Specified Character:", K)
# printing output
print("After replacing vowels with the specified character:",
replaceVowelsWithK(input_str, K))
Given Sting: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks
输出:
Given Sting: Geeks for Geeks
Given Specified Character: #
Afer replacing vowels with the specified character: G##ks f#r G##ks
方法#2:使用嵌套循环
在这里,我们首先将给定的字符串转换为一个列表,然后创建一个元音列表和一个空列表,即new_string 。在比较string_list 和元音列表的元素之后,如果元素是元音,则将K附加到new_string否则附加给定字符串的元素。
蟒蛇3
# Function to Replace each vowel
# in the string with a specified character
def replaceVowelsWithK(test_str, K):
# creating list of vowels
vowels_list = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
# creating empty list
new_string = []
# converting the given string to list
string_list = list(test_str)
# running 1st iteration for
# comparing all the
# characters of string with
# the vowel characters
for char in string_list:
# running 2nd iteration for
# comparing all the characters
# of vowels with the string character
for char2 in vowels_list:
# comparing string character
# and vowel character
if char == char2:
# if condition is true then adding
# the specific character entered
# by the user in the new list
new_string.append(K)
break
# else adding the character
else:
new_string.append(char)
# return the converted list into string
return(''.join(new_string))
# Driver Code
# input string
input_str = "Geeks for Geeks"
# specified character
K = "#"
# printing input
print("Given Sting:", input_str)
print("Given Specified Character:", K)
# printing output
print("After replacing vowels with the specified character:",
replaceVowelsWithK(input_str, K))
Given Sting: Geeks for Geeks
Given Specified Character: #
After replacing vowels with the specified character: G##ks f#r G##ks