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 Program to Replace occurrences by K except first character
Python provides several methods to replace all occurrences of a character except the first occurrence. This is useful when you want to preserve the first instance while modifying subsequent occurrences.
In Python, strings are immutable sequences of characters. When we need to replace specific occurrences, we can use built-in functions like slicing() and replace() or implement custom logic using loops.
Problem Statement
Given an input string and a replacement character k, replace all occurrences of the first character except the very first occurrence.
Example
For the string 'blueforblueforblue' with replacement character '*' ?
# Input inputString = 'blueforblueforblue' k = '*' # Expected Output: 'bluefor*luefor*lue' # The first 'b' remains, other 'b's become '*'
Method 1: Using Slicing and replace()
This approach slices the string from the second character and replaces all occurrences of the first character, then concatenates with the original first character ?
Syntax
string.replace(old, new, count)
Example
# input string
inputString = 'blueforblueforblue'
print("Input string:", inputString)
# replacement character
k = '*'
# replace all occurrences except first character
resultantStr = inputString[0] + inputString[1:].replace(inputString[0], k)
print("Resultant string after replacing:", resultantStr)
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Method 2: Using Double replace()
This method first replaces all occurrences, then restores the first character back ?
# input string
inputString = 'blueforblueforblue'
print("Input string:", inputString)
# replacement character
k = '*'
# replace all, then restore first occurrence
resultantStr = inputString.replace(inputString[0], k).replace(k, inputString[0], 1)
print("Resultant string after replacing:", resultantStr)
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Method 3: Using For Loop
This approach iterates through each character and builds the result string based on conditions ?
# input string
inputString = 'blueforblueforblue'
print("Input string:", inputString)
# replacement character
k = '*'
# initialize empty result string
resultantStr = ""
first_char_found = False
# traverse through each character
for c in inputString:
# if current character equals first character
if c == inputString[0]:
if not first_char_found:
# keep the first occurrence
resultantStr += c
first_char_found = True
else:
# replace subsequent occurrences
resultantStr += k
else:
# add other characters unchanged
resultantStr += c
print("Resultant string after replacing:", resultantStr)
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Slicing + replace() | O(n) | O(n) | High |
| Double replace() | O(n) | O(n) | Medium |
| For Loop | O(n) | O(n) | Low |
Conclusion
The slicing with replace() method is the most readable and efficient approach. Use the for loop method when you need more control over the replacement logic, or the double replace() method for a compact one-liner solution.
