Replace function in python

This tutorial helps us understand how to use replace function in python

Replace function in python

The replace() method replaces each matching occurrence of the old sub-string/old text in the string with the new sub-string/new text.

Syntax

'string'.replace(old,new,count)

Here,

  • old : Old substring to be replaced
  • new: New substring that replaces old substring
  • count : Optional parameter which depicts the number of times old substring to be replaced by new substring in the given string. If count is given as 0, we get the copy of the given string without any replacement.

Example

 

    #replacing 'h' with 'c'
    text='hat'
    print('character replacement--',text.replace('h','c'))

    #replacing 'catch' with 'throw'
    string='catch the ball'
    print('string replacement--',string.replace('catch','throw'))

    #replacing 'cream' with 'tream' for multiple times
    string2='I scream, you scream, we all scream for ice cream'
    print('multiple substring replacement--',string2.replace('cream','tream',3))

 

Output

 

        character replacement--cat
        string replacement--throw the ball
        multiple substring replacement--I stream, you stream, we all stream for ice cream

 

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.