Replace a String character at given index in Python Last Updated : 16 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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. Python s = "hello" idx = 1 replacement = "a" res = s[:idx] + replacement + s[idx+1:] print(res) Outputhallo Explanation:The string is split into two parts: everything before the target index (text[:index]) and everything after (text[index+1:]).The replacement character is inserted between these slices.This method is simple, efficient, and works well for small or large strings.Using list conversionConverting the string to a list allows us to modify its characters directly before converting it back. Python s = "hello" idx = 1 replacement = "a" a = list(s) a[idx] = replacement res = ''.join(b) print(res) Outputhallo Explanation:The string is converted to a list because lists are mutable.We modify the character at the target index and then rejoin the list into a string.This method is slightly less efficient but useful for multiple modifications.Using regular expressionsRegular expressions can be used to replace a character at a specific position by matching patterns. Python import re s = "hello" idx = 1 replacement = "a" # Create a pattern to match the character at the specific index pattern = f"^(.{{{idx}}})." res = re.sub(pattern, rf"\1{replacement}", s) print(res) Outputhallo Explanation:The pattern matches the character at the desired index using lookbehind.The re.sub() method replaces the matched character with the specified replacement.This method is powerful but less efficient and more complex for simple replacements.Using a manual loopWe can iterate through the string and build a new one, replacing the character at the given index. Python s = "hello" idx = 1 replacement = "a" res = "" for i in range(len(s)): if i == idx: res += replacement else: res += s[i] print(res) Outputhallo Explanation:The loop iterates through the string and appends characters to a new string.At the target index, the replacement character is added instead.Related Articles:Python StringString Slicing in PythonRegex Tutorial Loops in Python Comment More infoAdvertise with us Next Article Replace a String character at given index in Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads 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 Remove Character in a String at a Specific Index in Python Removing a character from a string at a specific index is a common task when working with strings and because strings in Python are immutable we need to create a new string without the character at the specified index. String slicing is the simplest and most efficient way to remove a character at a 2 min read Find position of a character in given string - Python Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of the character 'e' is at index1. Let's look at various metho 2 min read Python - Replace all numbers by K in given String 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 "he 2 min read Python - Replace multiple characters at once Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace mult 2 min read Reverse Alternate Characters in a String - Python Reversing alternate characters in a string involves rearranging the characters so that every second character is reversed while maintaining the original order of other characters. For example, given the string 'abcde', reversing the alternate characters results in 'ebcda', where the first, third and 3 min read Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det 2 min read Python | Remove given character from Strings list Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is 8 min read Python - Access element at Kth index in given String Given a String, access element at Kth index. Input : test_str = 'geeksforgeeks', K = 4 Output : s Explanation : s is 4th element Input : test_str = 'geeksforgeeks', K = 24 Output : string index out of range Explanation : Exception as K > string length. Method #1 : Using [] operator This is basic 4 min read Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in 2 min read Like