Advertisement



< Prev
Next >



Python - String replace() Method





As per the name suggests, the replace() string method is used to replace all the occurrences of a particular value in a string with a specific value.




  • Signature of replace() method


  • # Signature of replace() method
      
    replace(oldvalue, newvalue, count)


    Parameters Description
    oldvalue
    oldvalue is a value which will be replaced by a newvalue. A non-optional attribute.

    newvalue
    newvalue is a value which will replace the value specified by oldvalue. A non-optional attribute.

    count
    count is the number of occurrences of oldvalue to replace with the newvalue, by default all the occurrences of oldvalue are replaced by the newvalue. An optional attribute.





    Note :


    If the value to be replaced in a string is not found then string returned by the replace() method is the same as the string that invoked replace() method.




    Calling the replace() method on a string object.


    # Python -  Program to replace a particular value with another value in a string using replace() method.
    
    
    # Creating a string
    s = 'The Power of Python'
    
    print('Original String is : ', s)
    
    
    # Calling the replace() method to replace P with X in the invoked string.
    print('String\'s value after replacing P with X : ', s.replace('P', 'X')) 


    Output -


    Original String is :  The Power of Python
    String's value after replacing P with X :  The Xower of Xython


    Program Analysis


    Here in the next example, we have initialized a string with a value The Power of Python and we are calling on replace() method to replace all occurrences of P with X.





    Method replace() does not modify the content of invoked String object


    The method replace() only returns the content of string object after replacing an existing value in it with another value, without modifying its original content..

    # Python -  Program to replace a particular value with another value in a string using replace() method.
    
    
    # Creating a string
    s = 'Never give up!'
    
    
    # Priting the original string
    print('Original String is : ', s)
    
    
    # Calling the replace() method to replace e with E in the invoked string.
    print('String\'s value after replacing e with E : ', s.replace('e', 'E'))
    
    
    # Checking the original value of the string after calling replace () on it
    print('String\'s value after replace() is called : ', s)


    Output -


    Original String is :  Never give up!
    String's value after replacing e with E :  NEvEr givE up!
    String's value after replace() is called :  Never give up!


    Program Analysis


    We have initialized a string object with value Never give up! After calling the replace('e', 'E') method, the string object still has the same value Never give up! because replace() method doesn not modify the invoked string object.




    Strings are immutable but reference variables are mutable.


    String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the replace() method on the original object, we can make the reference variable to the original string, point to the result of replacing a character with another returned by the replace() method(but in this case, the original string will be lost).

    # Python -  Program to replace a particular value with another value in a string using replace() method.
    
    
    # Creating a string
    s = 'Being around nature is an amazing feeling!'
    
    
    # Priting the original string
    print('Original String is : ', s)
    
    
    # Calling the replace() method to replace e with E in the invoked string.
    # Reference to the old string is now pointing to the new string returned by calling the replace()
    # Old string is lost and not referenced by any reference variable anymore.
    s = s.replace('e', 'E')
    
    
    # Printing the new value referenced by the reference variable
    print('String\'s value after replace() is called : ', s)


    Output is :


    
    Original String is :  Being around nature is an amazing feeling!
    String's value after replace() is called :  BEing around naturE is an amazing fEEling!



    Advertisement




  • Replacing only a few occurrences of a value with another value.


  • While replace a oldvalue in the content of the invoked string with a newvalue, we can even specify an exact number of occurrences of oldvalue which will be replaced by the newvalue. Let us see an example.

    Note : The replace() method performs a case-sensitive search and replacement of a value with another value.

    # Python -  Program to replace a particular value with another value in a string using replace() method.
    
    
    # Creating a string
    s = 'The Power of Python'
    
    print('Original String is : ', s)
    
    
    # Calling the replace() method to replace 1 occurrence of P with p in the invoked string.
    print('String\'s value after replacing 1 occurrence of P with p : ', s.replace('P', 'p', 1))
    
    
    
    # Calling the replace() method to replace 2 occurrence of o with O in the invoked string.
    print('String\'s value after replacing 1 occurrences of o with O : ', s.replace('o', 'O', 2))
    
    
    # Calling the replace() method to replace 1 occurrence of e with E in the invoked string.
    print('String\'s value after replacing 1 occurrence of e with E : ', s.replace('e', 'E', 1))


    Output is :


    Original String is :  The Power of Python
    String's value after replacing 1 occurrence of P with p :  The power of Python
    String's value after replacing 1 occurrences of o with O :  The POwer Of Python
    String's value after replacing 1 occurrence of e with E :  ThE Power of Python





    Please share this article -





    < Prev
    Next >
    < String ljust() Method
    String count() Method >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement