
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
Case Insensitive String Replacement Using Python Program
In this article, we will learn Case insensitive string replacement in python.
Methods Used
The following are the various methods to accomplish this task ?
Using re.IGNORECASE, re.escape(), re.sub()
Using re.sub(), lambda, re.escape() functions
Using split(), lower() & replace() functions
Using split(), list() and join() functions
Method 1: Using re.IGNORECASE, re.escape(), re.sub()
re.compile() function
A regular expression pattern can be combined with pattern objects, which can then be used for pattern matching. This function also allows searching for a pattern again without rewriting it.
Syntax
re.compile(pattern, repl, string)
re.sub() function
The string with replaced values is returned by the re.sub() function, which stands for a substring. When we use this function, we can replace several elements with a list.
Syntax
re.sub(pattern, repl, string, count=0, flags=0)
re.escape() function
This function returns a string with all non-alphanumerics backslashed. If you want to match an arbitrary literal string that might contain regular expression metacharacters this function can be used.
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task -
Use the import keyword to import the re(regex) module.
Create a variable to store the input string.
Print the input string.
Create another variable to store the input replace string to be replaced with.
Initialize a substring to be replaced.
Ignore all the cases of the given string using the compile(), escape(), and IGNORECASE attributes (re.IGNORECASE is used to ignore cases).
Substitute the substring with replacing a string using the regex sub() function
Print the resultant string after replacing the case-insensitively.
Example
The following program returns a string after case-insensitive string replacement using re.IGNORECASE, re.escape(), re.sub() functions -
# importing re(regex) module import re # input string inputString = "hello TuTorialsPOint python" # printing input string print("Input String: ", inputString) # input replace string to be replaced with replaceString = "java" # substring to be replaced subString = "tutorialspoint" # compilation step to escape the word for all cases # the re.IGNORECASE is used to ignore cases compileObj = re.compile(re.escape(subString), re.IGNORECASE) #Substitute the substring with replacing a string using the regex sub() function resultantStr = compileObj.sub(replaceString, inputString) # printing resultant string after replacing print("Resultant string after replacing: ", resultantStr)
Output
On execution, the above program will generate the following output -
Input String: hello TuTorialsPOint python Resultant string after replacing: hello java python
Method 2: Using re.sub(), lambda, re.escape() functions
lambda function
A lambda function is a small anonymous function.
A lambda function can have an unlimited/any number of arguments but only one expression.
Syntax
lambda arguments : expression
Example
The following program returns a string after case-insensitive string replacement using re.sub(), lambda, re.escape() functions -
# importing re(regex) module import re # input string inputString = "hello TuTorialsPOint python" # printing input string print("Input String: ", inputString) # input replace string to be replaced with replaceString = "java" # substring to be replaced subString = "tutorialspoint" resultantStr = re.sub('(?i)'+re.escape(subString), lambda k: replaceString, inputString) print("Resultant string after replacing: ", resultantStr)
Output
On execution, the above program will generate the following output -
Input String: hello TuTorialsPOint python Resultant string after replacing: hello java python
Method 3: Using split(), lower() & replace() functions
split() ? splits a string into a list. We can define the separator; the default separator is any whitespace.
lower() ? converts all uppercase characters in a string to lowercase characters
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)
Example
The following program returns a string after case-insensitive string replacement using split(), lower() & replace() functions -
# input string inputString = "hello TuTorialsPOint python" # printing input string print("Input String: ", inputString) # input replace string to be replaced with replaceString = "java" # substring to be replaced subString = "tutorialspoint" # splitting input string into a list of words wordsList = inputString.split() # traversing through each word of words list for word in wordsList: # checking whether the current word is equal to the given substring # by converting them into lowercase using the lower() function if(word.lower() == subString.lower()): # replacing current word with the input replace string inputString = inputString.replace(word, replaceString) print("Resultant string after replacing: ", inputString)
Output
On execution, the above program will generate the following output -
Input String: hello TuTorialsPOint python Resultant string after replacing: hello java python
Method 4: Using split(), list() and join() functions
join() ? join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
list() function(converts the sequence/iterable to a list).
Example
The following program returns a string after case-insensitive string replacement using split(), list(), and join() functions -
# input string inputString = "hello TuTorialsPOint python" # printing input string print("Input String: ", inputString) # input replace string to be replaced with replaceString = "java" # substring to be replaced subString = "tutorialspoint" # splitting input string into a list of words wordsList = inputString.split() # traversing through index and word of @@ words list for index, word in enumerate(wordsList): # converting current word into lowercase and checking # whether it is equal to substring if word.lower() == subString: # replacing that word with the given replace string wordsList[index] = replaceString # converting words of wordlist into a string resultantStr = " ".join([word for word in wordsList]) print("Resultant string after replacing: ", resultantStr)
Output
Input String: hello TuTorialsPOint python Resultant string after replacing: hello java python
Conclusion
In this article, we learned how to use 4 different methods to case-insensitively replace the given string. Additionally, we learned how to use the regex module's IGNORECASE attribute to ignore the string's cases.