Sling Academy
Home/Python/Working with the str.replace() method in Python

Working with the str.replace() method in Python

Last updated: June 01, 2023

Syntax and Parameters

The str.replace() method in Python is used to replace occurrences of a specified substring within a string with another substring. It was added to the language from the early days and still is one of the most used string methods nowadays.

Syntax:

string.replace(old, new, count)

Where:

  • string: The original string on which the replacement operation is performed.
  • old: The substring to be replaced.
  • new: The substring that replaces the occurrences of the old substring.
  • count (optional): Specifies the maximum number of replacements to be made. If not provided, all occurrences of the old substring are replaced.

The replace() method searches for the old substring in the string and replaces it with the new substring. It returns a new string with the replacements applied, leaving the original string unchanged.

Examples

Some examples of using the str.replace() method in Python.

Basic Replacement

In this example, we will Replace a substring with another substring in a string:

text = "Hello, Sling Academy!"
new_text = text.replace("Hello", "Hi")
print(new_text) 

Output:

Hi, Sling Academy!

Multiple Limited Replacements

In this example, we will replace a substring with another substring in a string, but only a limited number of times.

text = "hello world, hello world, hello world, hello world, hello world"

# replace only the first three occurrences
new_text = text.replace("hello", "hi", 3)
print(new_text)

Output:

hi world, hi world, hi world, hello world, hello world

Multiple Replacements

This example demonstrates chaining replace() calls to perform multiple replacements:

text = "Welcome to Sling Academy!"

text = text.replace("!","").replace(" ", "_")
print(text)

Output:

Welcome_to_Sling_Academy

That’s it. Happy coding & have a nice day!

Next Article: Python: 4 Ways to Remove Substrings from a String

Previous Article: Python: How to check if a string is empty

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots