Skip to content
Related Articles

Related Articles

Python | Substitute character with its occurrence

View Discussion
Improve Article
Save Article
  • Last Updated : 09 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes, while working with Python, we can have a problem in which we need to substitute a character with its occurrence in a string. This a peculiar problem but can have application in many domains. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is brute way to solve the problem. In this, we run a loop for each character in string and perform the substitution while increasing the counter each time. 

Python3




# Python3 code to demonstrate working of
# Substitute character with its occurrence
# Using loop
 
# initializing string
test_str = "geeksforgeeks is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing letter
test_let = 'g'
 
# Substitute character with its occurrence
# Using loop
res = ''
count = 1
for chr in test_str:
    if chr == test_let:
        res += str(count)
        count += 1
    else:
        res += chr
 
# printing result
print("The string after performing substitution : " + str(res))

Output : 

The original string is : geeksforgeeks is best for geeks
The string after performing substitution : 1eeksfor2eeks is best for 3eeks

  Method #2 : Using lambda + regex + next() The combination of above functions can be used to perform this task. In this we perform the task of iteration using lambda, the regex and next() is used to perform the task of count iteration and finding the target char. 

Python3




# Python3 code to demonstrate working of
# Substitute character with its occurrence
# Using lambda + regex + next()
from itertools import count
import re
 
# initializing string
test_str = "geeksforgeeks is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing letter
test_let = 'g'
 
# Substitute character with its occurrence
# Using lambda + regex + next()
cnt = count(1)
res = re.sub(r"g", lambda x: "{}".format(next(cnt)), test_str)
 
# printing result
print("The string after performing substitution : " + str(res))

Output : 

The original string is : geeksforgeeks is best for geeks
The string after performing substitution : 1eeksfor2eeks is best for 3eeks

Method #3: Using count() method and slicing

Python3




# Python3 code to demonstrate working of
# Substitute character with its occurrence
 
# initializing string
test_str = "geeksforgeeks is best for geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing letter
test_let = 'g'
 
# Substitute character with its occurrence
res = ''
for i in range(0,len(test_str)):
    if(test_str[i]==test_let):
        res+=str(test_str[:i].count(test_let)+1)
    else:
        res+=test_str[i]
 
# printing result
print("The string after performing substitution : " + str(res))

Output

The original string is : geeksforgeeks is best for geeks
The string after performing substitution : 1eeksfor2eeks is best for 3eeks

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!