Python - Replace vowels in a string with a specific character K Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The task is to replace all the vowels (a, e, i, o, u) in a given string with the character 'K'. A vowel can be in uppercase or lowercase, so it's essential to account for both cases when replacing characters. Using str.replace() in a LoopUsing str.replace() in a loop allows us to iteratively replace specific substrings within a string. This method is used when we need to perform multiple replacements or when the substrings to be replaced vary based on certain conditions during the loop. Python # Input string s = "hello world" # Define the vowels and the replacement character v = "aeiouAEIOU" r = "K" # Replace each vowel with the replacement character for vowel in v: s = s.replace(vowel, r) # Print the result print(s) OutputhKllK wKrld Explanationloop iterates over each vowel in the string v, and in each iteration, str.replace() replaces all occurrences of that vowel in the string s with the character r.After processing all the vowels, the string "hello world" becomes "hKllK wKrld", and the result is printed.Let's explore various other methods to Replace vowels in a string with a specific character K.Table of ContentUsing List ComprehensionUsing re.sub() Using List ComprehensionUsing list comprehension allows you to efficiently iterate through a string and apply a transformation, such as replacing characters, while creating a new list. This method is concise and often more readable than using a loop with replace(). Python # Input string s = "hello world" # Define the vowels and the replacement character v = "aeiouAEIOU" r = "K" # Replace vowels with the replacement character using list comprehension result = ''.join([r if char in v else char for char in s]) # Print the result print(result) OutputhKllK wKrld Explanation list comprehension iterates through each character in the string s. If the character is a vowel (i.e., it exists in v), it is replaced with r. Otherwise, the character remains unchanged.After joining the modified characters into a new string, the result is "hKllK wKrld", and it is printed.Using re.sub() Using re.sub() with regular expressions allows you to replace all occurrences of a pattern in a string. Python import re # Input string s = "hello world" # Define the vowels and the replacement character v = "aeiouAEIOU" r = "K" # Use regex to replace vowels with the replacement character result = re.sub(r'[aeiouAEIOU]', r, s) # Print the result print(result) OutputhKllK wKrld ExplanationThe re.sub() function uses a regular expression pattern r'[aeiouAEIOU]' to match any character that is a vowel (both lowercase and uppercase). It then replaces each match with the character r.The string "hello world" is transformed into "hKllK wKrld", and the result is printed. Comment More infoAdvertise with us Next Article Python - Replace vowels in a string with a specific character K S srishivansh5404 Follow Improve Article Tags : Python Python string-programs Practice Tags : python Similar Reads Replacing Characters in a String Using Dictionary in Python In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement. For example, given the string "hello world" and a dictionary {'h': 'H', 'o': 'O'}, the output would be "Hel 2 min read Python - Replacing Nth occurrence of multiple characters in a String with the given character Replacing the Nth occurrence of multiple characters in a string with a given character involves identifying and counting specific character occurrences.Using a Loop and find()Using a loop and find() method allows us to search for the first occurrence of a substring within each list element. This app 2 min read Remove character in a String except Alphabet - Python Sometimes, we may need to remove characters from a string except for alphabets. For example, given a string s, the task is to remove all characters except for the alphabetic ones (a-z, A-Z). Another example is if the input string is "Hello123!@#" ,the output should be "Hello". Let's explore differen 3 min read Python - Count and display vowels in a string Given a string, we need to write a Python program that counts and displays all the vowels present in the string. Let's explore different ways of counting and displaying vowels in a string.We need to identify all the vowels in the string both uppercase and lowercase.We should display each vowel found 3 min read Python - Replace all occurrences of a substring in a string Replacing all occurrences of a substring in a string means identifying every instance of a specific sequence of characters within a string and substituting it with another sequence of characters. Using replace()replace () method is the most straightforward and efficient way to replace all occurrence 2 min read Like