Skip to content
Related Articles

Related Articles

Python – Replace occurrences by K except first character

Improve Article
Save Article
  • Last Updated : 18 Oct, 2022
Improve Article
Save Article

Given a String, the task is to write a Python program to replace occurrences by K of character at 1st index, except at 1st index.

Examples:

Input : test_str = 'geeksforgeeksforgeeks', K = '@'
Output : geeksfor@eeksfor@eeks

Explanation : All occurrences of g are converted to @ except 0th index.

Input : test_str = 'geeksforgeeks', K = '#'
Output : geeksfor#eeks

Explanation : All occurrences of g are converted to # except 0th index.

Method #1 : Using slicing + replace()

In this, we perform task of replacing entire string from 2nd character with K of the character occurring at 1st index. The result is prefix joined by first character.

Python3




# Python3 code to demonstrate working of
# Replace occurrences by K except first character
# Using slicing + replace()
 
# initializing string
test_str = 'geeksforgeeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '$'
 
# replacing using replace()
res = test_str[0] + test_str[1:].replace(test_str[0], K)
 
# printing result
print("Replaced String : " + str(res))

Output:

The original string is : geeksforgeeksforgeeks
Replaced String : geeksfor$eeksfor$eeks

Method #2 : Using replace()

In this, replace() is called twice for task of replacing all occurrences, and then just reverse replace the first occurrence.

Python3




# Python3 code to demonstrate working of
# Replace occurrences by K except first character
# Using replace()
 
# initializing string
test_str = 'geeksforgeeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '$'
 
# replacing using replace()
res = test_str.replace(test_str[0], K).replace(K, test_str[0], 1)
 
# printing result
print("Replaced String : " + str(res))

Output:

The original string is : geeksforgeeksforgeeks
Replaced String : geeksfor$eeksfor$eeks

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3: Using loops

Python3




# Python3 code to demonstrate working of
# Replace occurrences by K except first character
 
# initializing string
test_str = 'geeksforgeeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '$'
x=""
for i in test_str:
    if i==test_str[0] and i not in x:
        x+=i
    elif i==test_str[0] and i in x:
        x+=K
    else:
        x+=i
# printing result
print("Replaced String : " + str(x))

Output

The original string is : geeksforgeeksforgeeks
Replaced String : geeksfor$eeksfor$eeks

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!