
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Occurrences by K Except First Character in Python
Python has in build functions like slicing() and replace() that can be used for replacing the occurrence by k except the first character.
In Python, strings are among the most often used types. These are easily made by simply surrounding letters in quotations. Python treats single quotes and double quotes the same way. Assigning a value to a variable and creating a string is really straightforward.
Example
Assume we have taken an input string and k. We will now replace all the k characters except the first in an input string with the input character using the above methods.
Input
inputString = 'blueforblueforblue'
Output
Resultant string after replacing: bluefor*luefor*lue
Using slicing and replace() functions
In this method we are going to use the combination of slicing and replace function to replace the occurrence. The replace function returns a copy of the string that replaces all occurrences of an old substring with another new substring.
Syntax
string.replace(old, new, count)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input string.
Print the input string.
Create another variable to store input k character for replacing.
Slice from the second character to the end of the string and replace the occurrences of the first character with the given symbol k.
Concatenate this result with the first character.
Print the resultant string after replacing it.
Example 1
The following program returns a string after replacing the occurrences by k except the first character using slicing and replace() functions -
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # replacing all the characters except the first character with the given k symbol resultantStr = inputString[0] + inputString[1:].replace(inputString[0], k) # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
Output
On executing, the above program will generate the following output -
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Example 2
The following program returns a string after replacing the occurrences by k except the first character using replace() functions -
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # Replacing using the replace() method resultantStr = inputString.replace( inputString[0], k).replace(k, inputString[0], 1) # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
Output
On executing, the above program will generate the following output -
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Using for loop
In this method we are going to take help of for loop and not operator of python to replace the occurrence by k except the first character. The not operator is aa logical operator that returns True if the statement/statements are not True else returns False.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input string.
Print the input string.
Create another variable to store input k character for replacing
Initialize an empty string for storing the resultant string.
Use the for loop to traverse through each character of an input string.
Use the if conditional statement to check whether the current character is equal to the first character of the input string with indexing and whether it is NOT present in the resultant string using the not operator.
Add that current character to the resultant string using the +(Concatenation) operator if the condition is true.
Use the elif conditional statement to check whether the current character is equal to the first character of the input string and whether it is present in the resultant string
Add input k to the resultant string using the + operator if the condition is true.
Else add the current character directly to the resultant string without modification.
Print the resultant string after replacing
Example
The following program returns a string after replacing the occurrences by k except the first character using for loop -
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # initilazing empty string for storing resultant string resultantStr= "" # travsering through each character of an input string for c in inputString: # checking whether the current character is equal to the first character # of the input string and it is NOT present in the resultant string if c==inputString[0] and c not in resultantStr: # add that current character to the resultant string resultantStr+=c # checking whether the current character is equal to the first character # of the input string and it is in the resultant string elif c==inputString[0] and c in resultantStr: # adding k to the resultant string resultantStr+=k else: # else adding that character directly without modifying resultantStr+=c # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
Output
On executing, the above program will generate the following output -
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
Conclusion
In this article we have learned how to Replace occurrences by K except first character using 3 different approaches. We learned how to use the slicing to slice across the string and replace its elements. Additionally, we learnt how to join or combine two strings using the concatenation operator (+).