YouTip LogoYouTip

Python3 String Replace

Python3 replace() Method | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

Python 3 Tutorial

Python3 Tutorial Python3 Introduction Python3 Environment Setup Python3 VScode Python3 Basic Syntax Python3 Basic Data Types Python3 Data Type Conversion Python3 Interpreter Python3 Comments Python3 Operators Python3 Numbers Python3 Strings Python3 Lists Python3 Tuples Python3 Dictionaries Python3 Sets Python3 Conditional Statements Python3 Loop Statements Python3 First Program Python3 Comprehensions Python3 Iterators and Generators Python3 with Python3 Functions Python3 lambda (Anonymous Function) Python Decorators Python3 Data Structures Python3 Modules Python __name__ Python3 Input and Output Python3 File Python3 OS Python3 Errors and Exceptions Python3 Object Oriented Python3 Namespace/Scope Python Virtual Environment Creation Python Type Hints Python3 Standard Library Overview Python3 Examples Python Quiz

Python3 Advanced Tutorial

Python3 Regular Expressions Python3 CGI Programming Python3 MySQL (mysql-connector) Python3 MySQL (PyMySQL) Python3 Network Programming Python3 SMTP Sending Email Python3 Multithreading Python3 XML Processing Python3 JSON Python3 Date and Time Python3 Built-in Functions Python3 MongoDB Python3 urllib Python uWSGI Installation and Configuration Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python Useful Resources Python AI Drawing Python statistics Python hashlib Python Quantitative Python pyecharts Python selenium Library Python Spider Python Scrapy Library Python Markdown Python sys Module Python Pickle Module Python subprocess Module Python queue Module Python StringIO Module Python logging Module Python datetime Module Python re Module Python csv Module Python threading Module Python asyncio Module Python PyQt Python for Loop Python while Loop

Python3 Strings Python3 Strings

Python3 Lists

Deep Dive

Web Design & Development

Web Services

Web Service

Scripting Languages

Scripts

Programming

Development Tools

Computer Science

Programming Languages

Software

Python3.x Python3 replace() Method

Python3 Strings Python3 Strings


Description

The replace() method replaces occurrences of a substring with another substring in a string. If a third parameter max is specified, the replacement will not exceed max times.

Syntax

Here is the syntax for the replace() method:

str.replace(old, new[, max])

Parameters

  • old -- The substring to be replaced.
  • new -- The new substring to replace the old substring.
  • max -- Optional. The maximum number of replacements to make.

Return Value

Returns a new string with all occurrences of the old substring replaced by the new substring. If the third parameter max is specified, the replacement will not exceed max times.

Example

The following example demonstrates the usage of the replace() function:

Example

#!/usr/bin/python3

str = "www.w3cschool.cc"
print("Old address of Tutorial: ", str)
print("New address of Tutorial: ", str.replace("w3cschool.cc", ""))

str = "this is string example....wow!!!"
print(str.replace("is", "was", 3))

The output of the above example is as follows:

Old address of Tutorial:  www.w3cschool.cc
New address of Tutorial:  www.
thwas was string example....wow!!!

Python3 Strings Python3 Strings

← Python3 String StripPython3 String Splitlines β†’