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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.