Thursday, October 1, 2015

Python Tutorial: Python Replace String Method #66



See our full tutorial on the Python Replace String Method at http://learnpythontutorial.com/python-replace-string-method/

Python Replace String Method

In this Python tutorial we are going to look at the Python replace string method. The python replace string method gives the ability to replace content in an existing string and create a new string object with the replaced content. The replace string method takes three arguments. First argument is the content that will be replaced and must be in a string format. The second argument is the content that will replace the existing content this argument must be in a string format as well. The third argument is how many occurrences will be replaced.



Python Replace String Method Syntax

'String Object'.replace('string content', 'replace content', number of occurrences)



'String Object' - Our string object where we are going to replace content.

.replace() - This string method replaces content in an existing string and creates a new string object.

'string content' -  The content in the existing string object that will be replaced. Must be in a string format.

'replace content' - This is the content that replace the content in an existing string object. Must be in a string format.

number of occurrences - How many occurrences would you like to be replaced. Default is replace all occurrences.

Examples Of The Python Replace String Method

#Example 1

 'This is a string'.replace('is', 'IS')

'ThIS IS a string'



#Example 2

 'This is a string'.replace('is', 'IS', 1)

'ThIS is a string'



#Example 3

 'Mississippi'.replace('i', 'I')

'MIssIssIppI'

Examples Explained



Example 1:



'This is a string'.replace('is', 'IS') -We create a string object and then call the replace string method on our string object. Our string method has two arguments. The first argument is the content we want to replace and the second argument is content we want to insert into our string object. We did not provide an occurrence argument on this string method.



'ThIS IS a string' - We are returned a string where two parts of our content that was replaced by with uppercase 'IS'.



Example 2:



'This is a string'.replace('is', 'IS', 1) - We create a string object and call the replace string method on our string object. We provide our replace string method with 3 arguments. First argument of 'is' is going to be replace with the second argument of 'IS' and then our third argument says only replace the first occurrence.



'ThIS is a string' - We are returned a new string object that contains the newly replaced part of the string.



Example 3:



'Mississippi'.replace('i', 'I') - We create a new string object and call the replace string method on the object.



'MIssIssIppI' - We are returned a new object where the lowercase 'i' is replaced with an uppercase 'I'.



Conclusion



In this tutorial we look at the Python replace string method which gives us the ability to replace parts of a string object and create a new object. If you have any questions please leave a comment below.

No comments:

Post a Comment