How to replace characters in a string in Python?

If you are looking for replacing instances of a character in a string, Python has a built-in replace() method which does the task for you.

The replace method replaces each matching occurrence of the old characters/substring with the new characters/substring.

Syntax : 

string.replace(old, new, count)

Parameters : 

  • old – character or substring you want to replace. 
  • new – a new character or substring which would replace the old substring.
  • count (optional)–  the number of times you want to replace the old substring with the new substring 

Return Value

This method returns a copy of the string, where the old substring is replaced with a new one, keeping the original string unchanged. If the old string is not found, the copy of the original string is returned.

Example 1 – Replace all the instances of a character in a given string

# replace all the characters in a string
sample_text = 'All the, commas, will, be, replaced  by empty string, in this,, sentence,'
print(sample_text.replace(',', ''))

Output

All the commas will be replaced  by empty string in this sentence

Example 2 – Replace all the instances of a substring in a given string


# replace the old substring with new substring
sample_text2 = 'This is a wrong sentence'
print(sample_text2.replace('wrong', 'correct'))

Output

This is a correct sentence

Example 3 – Replace only one instances of a substring in a given string


# replace substring of only one occurence
sample_text3 = 'int, string, bool, byte, string, float,  bit, string'
print(sample_text3.replace('string', 'char',1))

Output

int, char, bool, byte, string, float,  bit, string

Example 4 – Replace character in string at index

#  Replace character in string at index 
sample_text4 = 'Europx'
new_text ='e'
print(sample_text4[:5] + new_text + sample_text4[5+1:])

Output

Europe
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Powershell Concatenate String

PowerShell Concatenate String

Table of Contents Hide Introduction to PowerShell Concatenate StringHow to Concatenate Strings and Variables in PowerShell#1. Expanding Strings#2. Substitution or string.Format approach#3 Join Operator Microsft PowerShell scripting is little different from…
View Post
Python String Isalnum()

Python String isalnum()

Table of Contents Hide What is Alphanumeric?isalnum() Syntaxisalnum() Parameters isalnum() Return valueExample 1: Python string isalnum() working examplesExample 2: Program to check if the string is alphanumeric Python string isalnum() method…
View Post
Python String Center()

Python String center()

Table of Contents Hide center() Syntaxcenter() Parameter center() Return ValueExample 1: center() Method With Default fillcharExample 2: center() Method With * fillchar Example 3: Returns original string if the width is…
View Post