Home » Python » Python String replace Method

About Yatin Batra

Yatin Batra
Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.

Python String replace Method

Hello in this tutorial, we will understand the string replace() method in python programming.

1. Introduction

replace() method in python programming returns a copy of a string where all occurrences of a substring are replaced by the new string. The method also accepts an optional count argument only if the first count occurrences are to be replaced.

1
my_string.replace(old_string, new_string, count)

where,

  • old_string – Represents the substring you want to replace
  • new_string – Represents the new substring which would replace the old_string
  • count – Number of times to process the replace

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String replace Method

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.

2.1 String Replace Example

Let us understand this with the help of a code snippet.

String replace in Python

01
02
03
04
05
06
07
08
09
10
# replace() method
string = 'one one is a fastest animal, two two was one too.'
print('Original string = {}'.format(string))
print('\n')
# prints the string by replacing one by three
print(string.replace('one', 'three'))
print('\n')
# prints the string by replacing only 3 occurrences of one
count = 2
print(string.replace('one', 'three', count))

If everything goes well the following output will be shown in the IDE console.

Console Output

1
2
3
Original string = one one is a fastest animal, two two was one too.
three three is a fastest animal, two two was three too.
three three is a fastest animal, two two was one too.

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • An introduction to replace() method in the python programming language
  • Sample program to understand the replace() in the python programming language

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was an example of replace() method in python programming.

Download
You can download the full source code of this example here: Python String replace Method
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments