Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Multiple Indices Replace in String
When working with strings in Python, there are regular circumstances where we need to replace characters at specific positions (indices) within a string. This task requires an efficient approach to identify the indices of characters that need to be replaced and then modify them with the required values. Python offers different methods to accomplish this goal.
In this article, we will explore three different approaches to replace characters at multiple indices in a string. These approaches include using string slicing, converting to a list of characters, and utilizing regular expressions (regex). Each approach has its own advantages and can be applied based on the specific requirements of the task.
Multiple Indices Replace in String
The concept of replacing multiple indices in a string refers to the task of replacing specific characters at specified positions within a string. This is useful when you need to mask sensitive information, correct errors at known positions, or modify specific characters for formatting purposes.
Available Approaches
Approach 1 ? Using String Slicing
Approach 2 ? Using List Conversion and Joining
Approach 3 ? Using Regular Expressions
Approach 1: Using String Slicing
This approach uses string slicing to reconstruct the string with replacements at specific indices. We iterate through each index and build a new string by concatenating parts before the index, the replacement character, and parts after the index.
Algorithm
Define the original string and indices to replace
Iterate through each index
Use string slicing to replace character at each index
Output the modified string
Example
text = "Hello World! Hello Python!"
indices = [0, 6, 13]
for index in indices:
text = text[:index] + '#' + text[index+1:]
print("Modified string:", text)
Modified string: #ello #orld! #ello Python!
Approach 2: Using List Conversion and Joining
This approach converts the string to a list of characters, modifies characters at the specified indices, and then joins them back into a string. This method is more efficient for multiple replacements as it avoids creating intermediate strings.
Algorithm
Convert the string to a list of characters
Iterate over the indices and modify characters
Join the modified characters into a new string
Print the result
Example
text = "Hello World! Hello Python!"
indices = [0, 6, 13]
# Convert to list of characters
characters = list(text)
# Replace characters at specified indices
for index in indices:
characters[index] = '#'
# Join back to string
new_text = ''.join(characters)
print("Modified string:", new_text)
Modified string: #ello #orld! #ello Python!
Approach 3: Using Regular Expressions
Regular expressions provide a powerful way to search and replace patterns within strings. This approach creates a pattern that matches characters at specific positions and replaces them using the re.sub() function.
Algorithm
Import the re module
Define the string and indices to replace
Create a regex pattern for the characters at those indices
Use sub() function to replace matching characters
Example
import re
text = "Hello World! Hello Python!"
indices = [0, 6, 13]
# Get characters at specified indices and create pattern
chars_to_replace = [text[index] for index in indices]
pattern = '|'.join([re.escape(char) for char in chars_to_replace])
# Replace using regex
new_text = re.sub(pattern, '#', text)
print("Modified string:", new_text)
Modified string: #ello #orld! #ello Pyt#on!
Comparison of Approaches
| Approach | Time Complexity | Best For | Drawbacks |
|---|---|---|---|
| String Slicing | O(n×m) | Few replacements | Creates many intermediate strings |
| List Conversion | O(n) | Many replacements | Extra memory for list |
| Regular Expressions | O(n) | Pattern-based replacements | May replace unintended matches |
Conclusion
Use list conversion for efficient multiple index replacements. String slicing works well for few replacements, while regex is powerful for pattern-based replacements. Choose the approach based on your specific performance and functionality requirements.
