Python - Replace all numbers by K in given String Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We need to write a python program to replace all numeric digits (0-9) in a given string with the character 'K', while keeping all non-numeric characters unchanged. For example we are given a string s="hello123world456" we need to replace all numbers by K in the given string so the string becomes "helloKKKworldKKK". Using str.translate with a Translation Table str.translate() method in Python allows for replacing multiple characters in a string using a translation table. A translation table is typically created with str.maketrans(), which maps characters to their replacements. Python s = "hello123world456" # Replacing digits with 'K' using translate and maketrans res = s.translate(str.maketrans("0123456789", "K" * 10)) print(res) OutputhelloKKKworldKKK Explanation:str.maketrans creates a translation table mapping digits (0-9) to the letter K.Translate applies the translation to the string.Using Regular Expressions (re.sub)re.sub() method in Python allows for replacing parts of a string that match a regular expression pattern. It takes the pattern, the replacement string, and the original string as arguments. Python import re s = "hello123world456" # Replacing digits (\d) with 'K' using re.sub res = re.sub(r'\d', 'K', s) print(res) OutputhelloKKKworldKKK Explanation:Regex pattern \d matches any digit in the string.re.sub replaces each digit with K.Using List ComprehensionUsing list comprehension with string manipulation allows us to create a new list by applying an operation to each character or substring in a string. Python s = "hello123world456" # Replacing digits with 'K' using list comprehension res = ''.join(['K' if char.isdigit() else char for char in s]) print(res) OutputhelloKKKworldKKK Explanation:List comprehension iterates through each character in the string.If the character is a digit (char.isdigit()), it’s replaced with K; otherwise, it remains unchanged. Comment More infoAdvertise with us Next Article Python - Replace all numbers by K in given String M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Replace a String character at given index in Python In Python, strings are immutable, meaning they cannot be directly modified. We need to create a new string using various methods to replace a character at a specific index. Using slicingSlicing is one of the most efficient ways to replace a character at a specific index.Pythons = "hello" idx = 1 rep 2 min read Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for 2 min read Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num 2 min read Python - Add space between Numbers and Alphabets in String In this article, we delve into a practical Python solution to improve text readability by adding spaces between numbers and alphabets in strings by utilizing Python's powerful string manipulation capabilities. Input: test_str = 'ge3eks4geeks is1for10geeks' Output: ge 3 eks 4 geeks is 1 for 10 geeks 9 min read Python - Replace Different Characters in String at Once The task is to replace multiple different characters in a string simultaneously based on a given mapping. For example, given the string: s = "hello world" and replacements = {'h': 'H', 'o': '0', 'd': 'D'} after replacing the specified characters, the result will be: "Hell0 w0rlD"Using str.translate( 3 min read Like