Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

replace function in python

 replace function:

python having different types of in-built functions from them replace() is one of the function.
  • The replace() function name itself saying that "one string is replaced with the other string".
  • It consists of three parameters old value, new value, and count.
old value:  The value that we want to replace.

new value: The value we want to Replace with is the new value.

count: The number of times of the value we want to replace old with the new value.

Syntax: str.replace(old value,new value,count)

#write a python code to demonstrate replace function.

  1. a="my name is java"
  2. b=" she is a very talented person"
  3. c="They have no knowledge in python"
  4. a=a.replace("java","python")
  5. b=b.replace("talented person","brave girl")
  6. c=c.replace("no","more",3)
  7. print(a)
  8. print(b)
  9. print(c)
  • In the above program in line 5, we want to replace no with more and we gave the count as 3 so in the string where we have no that will be replaced with more up to 3 times only.
  • so the output will be as shown in below.
Output:  my name is python
               she is a very brave girl
              They have more kmorewledge in python 





This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

replace function in python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×