Python has a built-in function string.replace () to replace a string with another string. The
string.replace () function accepts the string to replace and the new string you want to replace the old one with.
string.replace (" old string "," new string ", count)
old string: string, which you want to replace.
new string: The new part of the string that you want to place at the position of the old string.
count: number of times.
Example 1: Replacing the old line with a new line passed to the function
inp_str = "Python with pythononline "res = inp_str.replace (" pythononline "," AP ") print (" Original String: ", inp_str) print (" Replaced String: ", res)
In the above code snippet, we replaced the string "pythononline" with "AP".
Original String: Python with pythononline Replaced String: Python with AP
Now let's use the count parameter to specify the number of instances of the row to which we want to replace.
Example 2: Using
count as a parameter to replace ()
inp_str =" abcdaaseweraa "res = inp_str.replace (" a "," x ", 2) print (" Original String: ", inp_str) print (" Replaced String: ", res)
In this example, we have passed the input string as && # 8216; abcdaaseweraa & # 8217 ;. In addition, we passed the character "a" of the original string, which must be replaced with the character "x".
Here the counter is set to 2, i.e. only the first two "a" characters encountered will be replaced with the "x" character. The rest of the letters "a" encountered will not be added later and will remain the same.
Original String: abcdaaseweraa Replaced String: xbcdxaseweraa
The replace () function can also be used to replace some string present in a csv or text file. p>
Python Pandas Module is useful for working with datasets. The
pandas.str.replace () function is used to replace a string with another string in a variable or data column.
dataframe.str.replace ('old string',' new string')
In the following example, we will use the following dataset:
import pandas df = pandas.read_csv ("C: / IMDB_data. csv ", sep =", ", encoding =' iso-8859-1') df ['Language'] = df [' Language'] .str.replace ("English", "Hindi")
In the above code snippet, the
pandas.read_csv () function is used to import and load the dataset.
As you can see from the dataset above, we have selected the Language column to replace "English" with "Hindi".
This book serves as a practical guide on how to utilize big data to store, process, and analyze structured data, focusing on three of the most popular Apache projects in the Hadoop ecosystem: Apache S...
The field of Artificial Intelligence (AI), which can definitely be considered to be the parent field of deep learning, has a rich history going back to 1950. While we will not cover this history in mu...
Roger Jennings is an author and consultant specializing in Microsoft .NET n-tier database applications and data-intensive Windows Communication Foundation (WCF) Web services with SQL Server. He’s be...
A Problem-Solver’s Guide to Building Real-World Intelligent Systems. Data is the new oil and Machine Learning is a powerful concept and framework for making the best out of it. In this age of aut...