Python Replace Character in String by Index
In this article, you are going to learn how to replace character in string by index in Python using 2 different methods. We will also look at the speed comparison of both methods.
Previously we have seen how to replace a character in a string in Python when the character is known. Here index of the character is used to replace the character in the string.
- Method 1: Converting String to List
- Method 2 ⭐️: Using string slicing
- Speed Comparison of both the methods
- Conclusion
Table of Contents
Method 1: Converting String to List
The string is immutable in Python. So, we can't directly replace a character in a string. It will throw an error.
To overcome this, you can convert the string to list and then replace the character in the list at the given index.
Finally, convert the list to string using the join() method and assign it to the original string.
# Method 1
# replace character in string by index
def replace_char(str, index, char):
# convert string to list
str = list(str)
# assign character to index
str[index] = char
# join the list and return the string
return ''.join(str)
str = "Hello World"
# replace character at index 0 with 'B'
print(replace_char(str, 0, 'T'))
# replace character at index 6 with 'B'
print(replace_char(str, 6, 'B'))
Tello World Hello Borld
Method 2: Using string slicing
Another way to replace a character in a string by the index is using string slicing.
String slicing is a technique to access a part of a string. It is done by specifying the start and end index of the substring. For example, str[0:5] will return the substring from index 0 to 5.
Using this we access the substring from index 0 to index-1 and concatenate it with the character and the substring from index+1 to the end of the string.
# Method 2:
# using string slicing
def replace_char(str, index, char):
# concatenate the substring from 0 to index-1 and character
# and substring from index+1 to the end of the string
str = str[:index] + char + str[index+1:]
# return the string
return str
str = "tutorials tonight"
# replace character at index 0 with 'T'
print(replace_char(str, 0, 'T'))
# replace character at index 10 with 'T'
print(replace_char(str, 10, 'T'))
Tutorials tonight tutorials Tonight
You can see in the above example, the character at index 0 is replaced with 'T' and the character at index 10 is replaced with 'T' using the string slicing method.
Speed Comparison of both the methods
Let's compare the speed of both methods and see which one is faster.
For this, we will use the time module. It provides various time-related functions.
Here we will use time.time() function which returns the number of seconds passed since January 1, 1970, 00:00:00 (UTC) at Unix systems.
So, we will calculate the time difference between the start and end time of the function to get the time taken by the function to execute.
import time
# Method 1: replace character in string by index
def replace_char1(str, index, char):
str = list(str)
str[index] = char
return ''.join(str)
# method 2: using string slicing
def replace_char2(str, index, char):
str = str[:index] + char + str[index+1:]
return str
# compare the time taken by both methods
def compare_time():
str = "Hello World"
index = 5
char = 'z'
t1 = time.time()
for i in range(1000000):
replace_char1(str, index, char)
t2 = time.time()
for i in range(1000000):
replace_char2(str, index, char)
t3 = time.time()
print("Time taken by method 1: ", t2-t1)
print("Time taken by method 2: ", t3-t2)
compare_time()
('Time taken by method 1: ', 0.6108551025390625) ('Time taken by method 2: ', 0.2126619815826416)
As you can see, the time taken by method 2 is less than method 1. So, method 2 ⭐️ is faster than method 1.
Conclusion
In this article, you have learned two methods to replace a character in a string by index.
Also, we found that method 2 is faster than method 1. So, you can preferably use method 2 to replace a character in a string by an index.
Happy Coding!😊