Member-only story

How to Replace Substring in a String Python| Python Tutorial

Include different methods with various examples

Sanjay Priyadarshi
4 min readOct 19, 2022
Photo by Anete Lūsiņa on Unsplash

As Python developers, we know that strings are ordered collections of characters.

We can use strings to represent and store text and byte-based information.

Immutable sequences

Strings are immutable sequences.

It means that no programmer can make changes to a string by directly assigning something to an index.

When a developer writes code in multiple languages, they often forget that strings in Python are immutable.

But any developer should remember that strings are immutable in python.

Example

str = "Hello"str[3] = 'y'print(str)
#Result:
TypeError: 'str' object does not support item assignment

How to make changes to a string

If strings are immutable, how can we make changes to the string?

To make changes to a string, you need to create a new string using different tools, such as concatenation and string methods, and then reassign it to the name of the old string.

--

--

Sanjay Priyadarshi
Sanjay Priyadarshi

Written by Sanjay Priyadarshi

Join a Community of People Who Love Javascript, Programming and Technology — codertoentrepreneurs.substack.com

No responses yet