![Python To String Python To String](https://tqwishes.com/wp-content/uploads/2017/09/python-programming-essentials-m7-strings-12-638.jpg)
Unlike in C and Java, Python variables don't contain explicit type information. If you're using a function that requires a string, you can pass variables of other types to it without throwing an exception, although your code may throw an exception when it tries to process the variable. String Formatting in Python. In Python, we can take advantage of two separate methods of string interpolation. String interpolation is a term used to describe the process of evaluating a string value that is contained as one or more placeholders. To put it simply, it helps developers with string formatting and concatenation.
replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.
Syntax :
Parameters :
old – old substring you want to replace.
new – new substring which would replace the old substring.
new – new substring which would replace the old substring.
count – the number of times you want to replace the old substring with the new substring. (Optional )
Return Value :
It returns a copy of the string where all occurrences of a substring is replaced with another substring.
It returns a copy of the string where all occurrences of a substring is replaced with another substring.
Below is the code demonstrating replace() :
# use of replace() method string = 'geeks for geeks geeks geeks geeks' # Prints the string by replacing geeks by Geeks # Prints the string by replacing only 3 occurence of Geeks print (string.replace( 'geeks' , 'GeeksforGeeks' , 3 )) |
Output :
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the 'Improve Article' button below.