Open In App

Python – Replace occurrences by K except first character

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Time Complexity: O(n), where n is length of test_str string.

Auxiliary Space: O(n), where n is length of string x to store the result.

Method #4: Using list comprehension

The given program replaces all occurrences of the first character in the given string with a new character K except for the first occurrence. Here is the step-by-step approach to implement this program in Python:

  • Initialize the input string – test_str.
  • Print the original string.
  • Initialize a new character K with any desired character value.
  • Using list comprehension, iterate through each character of the input string except for the first character. If the character is equal to the first character and it is not the first occurrence, then replace it with the new character K, else keep the original character. Also, add the first character of the input string as it is in the beginning.
  • Join the resulting list of characters into a single string.
  • Print the resulting string.

Python3




# Python3 code to demonstrate working of
# Replace occurrences by K except first character
# Using list comprehension
  
# initializing string
test_str = 'geeksforgeeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing K
K = '$'
  
# using list comprehension
res = ''.join([test_str[0]] + [K if i == test_str[0] and j!=0 else i for j, i in enumerate(test_str[1:])])
  
# printing result
print("Replaced String : " + str(res))


Output

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

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #5: Using enumeration

  1. Initialize the input string test_str and the replacement character K.
  2. Initialize an empty string x.
  3. Iterate over each character in test_str.
  4. If the current character is the same as the first character of the input string and it is not already present in the result string x, append it to x.
  5. If the current character is the same as the first character of the input string and it is already present in the result 
  6. If the current character is not the same as the first character of the input string, append it to x
  7. Print the resulting string

Python3




# initializing string
test_str = 'geeksforgeeksforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '$'
 
# using enumerate
x = ""
for idx, char in enumerate(test_str):
    if idx == 0:
        x += char
    else:
        x += K if char == test_str[0] else char
 
# printing result
print("Replaced String : " + str(x))
#This code is contributed by vinay Pinjala.


Output

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

Time complexity: The code iterates over the input string once and performs constant time operations for each character. Therefore, the time complexity of the code is O(n), where n is the length of the input string.

Auxiliary Space: The code initializes a string x of the same length as the input string. Therefore, the space complexity of the code is O(n), where n is the length of the input string.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads