TechBeamers
    • Python
      • Python Basic
      • Python OOP
      • Python Quizzes
      • Python Examples
      • Python Selenium
      • Python Advanced
    • Java
      • Java Basic
      • Java Flow Control
      • Java OOP
      • Java Quiz
      • Java Interview
    • C
    • C#
    • SQL
    • MySQL
    • Selenium
      • Selenium Tutorial
      • TestNG Tutorials
      • Selenium Interview
      • Selenium Quiz
    • Testing
      • Software Testing
      • Manual Testing
      • Software Testing Quiz
      • Testing Interview Questions
    • Agile
    • More
      • Android
      • Angular
      • Linux
      • Web Dev
      • Best IDEs
      • Front-End Quiz
      • ShellScript Quiz
      • C++ Quiz
      • IOT
      • General
    TechBeamers
    Python Basic

    Python String Replace with Examples

    Updated:Sep 29, 20234 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    String replacement is a common task in Python. A simple yet powerful way to replace a string in Python is by using the Python string replace() method. In today’s post, we’ll not only explain this method but will understand it from a practical point of view.

    Python is one of the best scripting languages that most easily integrates with Selenium Webdriver for web-based automation, which requires a lot of string handling. There you may need to use string handling for building dynamic XPath, comparing dates, and searching for substrings. Hence, it’s inevitable to avoid using the Python string replace() method.

    Replacing a String in Python

    • How to replace a string in Python
    • Python replace() method signature
    • Examples of string replacement in Python

    Nowadays, Python is also becoming popular for use in data analytics applications. Here you first need to process the data and then have to represent it in graphs. And, all such operations require string manipulation using methods like the Python string replace(). Next, apart from knowing about these methods, you also have to think creatively about using string functions. So, you can use them more efficiently.

    Define Python String Replace Method with Examples
    Define Python String Replace Method with Examples

    In one of our earlier tutorials, we discussed Python Strings at great length. Perhaps you should read this post if you wish to learn strings from depth. However, to evaluate yourself for how logically you can use strings, please try this Python string quiz.

    How to replace a string in Python

    Often you’ll get to deal with a string (str object) in Python to modify its contents by replacing a part of the text with another.

    In Python, everything is an object which is also true for strings. It means that str is a string object. And, Python’s string module provides a replace() method. We can call it in one of the following ways.

    1- Calling replace() with the str object prefixing with a dot.
    2- Call it directly after importing the string module.

    Python replace() method signature

    The <string.replace()> method has the following syntax.

    str.replace(s, old, new[, replacefreq])

    Here is a summary of the parameters passed to the method.

    ParametersDescription
    <s>It’s the value of a string to search and replace from.
    <old>It’s the value of the old sub-string you like to replace.
    <new>It’s the value of the new substring that you want to replace with the old one.
    <replacefreq>It represents the number of times the string will get replaced.

    Examples of string replacement in Python

    In order to provide you with a practical experience, we included the following examples. Try to write the code from these into your IDE, review, and run.

    Example 1: Using the module name to call replace()

    oldString = 'I love Python 2.0'
    
    import string
     
    newString = string.replace(oldString, '2.0', '3.0')
    print(newString)
     
    newString = string.replace(oldString, '2.0', '3.0.')
    print(newString)
     
    oldString = 'Are you a tester who tests websites? Be a good tester.'
    newString = string.replace(oldString, 'test', 'develop', 1)
    print(newString)
    
    newString = string.replace(oldString, 'test', 'develop', 2)
    print(newString)
    
    newString = string.replace(oldString, 'test', 'develop', 3)
    print(newString)

    The above Python code will produce the following output.

    I love Python 3.0
    I love Python 3.0.
    Are you a developer who tests websites? Be a good tester.
    Are you a developer who develops websites? Be a good tester.
    Are you a developer who develops websites? Be a good developer.

    Example 2: Using the <str> object to call replace()

    oldString = 'I love Python 2.0'
     
    newString = oldString.replace('2.0', '3.0')
    print(newString)
     
    newString = oldString.replace('2.0', '3.0.')
    print(newString)
     
    oldString = 'Are you a tester who tests websites? Be a good tester.'
    newString = oldString.replace('test', 'develop', 1)
    print(newString)
    
    newString = oldString.replace('test', 'develop', 2)
    print(newString)
    
    newString = oldString.replace('test', 'develop', 3)
    print(newString)

    And here, it’s the output of the above Python string replace() example.

    I love Python 3.0
    I love Python 3.0.
    Are you a developer who tests websites? Be a good tester.
    Are you a developer who develops websites? Be a good tester.
    Are you a developer who develops websites? Be a good developer.

    From both of the above examples, it’s evident that you can use any of two methods to replace strings. Though, it’s the second method (in example 2) that is more convenient to use.

    Must read – How to replace chars/substrings in Python string

    Summary

    Hopefully, the above post will have given you a fair idea of using the string replace() method in Python. You can learn more through practice, get the code from the examples, and start.

    If you want us to cover any other topic in Python, then please write to us.

    All the Best,

    TechBeamers

    Previous Article5 Must-have Chrome Extensions for Python
    Next Article Seven Python Compare String Methods for Dummies
    Join Us!
    Loading
    Latest Tutorials

    Floating Point Numbers in Python

    Sep 28, 2023

    Python String Splitting: split(), rsplit(), regex

    Sep 27, 2023

    Java IRC Bot

    Sep 26, 2023

    IRC Bot Explained

    Sep 26, 2023
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    • Terms of Use
    © 2023 TechBeamers. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.