How to replace one string to another in python?

How to replace one string to another in python?

Little introduction

As a Deepak Chopra pointed out,

> Replace your self-defeating thoughts with self supporting thoughts.

This thought showing the power of replacement in our life.

Likewise, Python has the power of string replacement. Where you can replace one string to another string.

For analogy, the string "I love dog" replace with "I love cat". Here, you can see that 'dog' has replaced with 'cat'.

The 'cat' and 'dog' are substring in string.

Thus, you can say python string replace the one substring to another substring.

I'm going into the depth of string replacement with this blog post.

This blog post going to show you the two methods of string replacement in python.

These methods are useful for string replacement.

So, stay tune with me.

And if you feel about it then use it. Otherwise, tell me your problem, I'll solve it.

Replace python string using: 1st method

The first method is old_string.replace() method.

In this method you don't have to import the string module.

For example,

image.png String replacement

I'm going to take you step-by-step in this code.

Step1: I've created a variable--> statement and stored string value to it.

Step2: Second, I'm going to apply .replace() method on it. And this action is going to store in new variable new_stat.

Step3: At last, you can display the variable new_stat using print() statement.

Below I'm going to break it down into 3 section:

1. Syntax 2. Parameters 3. Return statement

So, let's take a approach to each one of this.

Syntax of replace

First of all, the syntax is old_string.replace(a,b).

Here, I've used .replace to action on old_string.

Parameters for python string replacement

The .replace(a,b) has two parameters.

First one is a substring which I have to replace.

Second is b which I want in new statement.

At last, you can say that it has two parameter. one is old and another is new.

Return value---> new substring

What's the return value of it?

The return value is String with new substring.

In above, you've seen the return value "I love cat ".

A "dog" has replaced with "cat".

From this, you can take straightforward idea of string replacement.

2nd Method to string replacement

You can use this method in python 2.x.

Thus, It's somewhat little big for you.

So, what's the steps to replace the string?

First of all, you've to create a variable.

Second, you've to import the string module.

Third, you've to store string.replace(old_string,the_pattern,into convert) in new_string. At last, you'll run the code and display the output.

For example,

image.png

--->I love python 3.0.

Here, you can observe that 2.0 has replaced with 3.0.

Syntax of second method

The syntax of second method is string.replace(old_string,replace_string, to_replace_in, how many times).

Here, you can see that I've placed old_string as parameter.

In first method, old_string is an object where replace method attached.

Parameters for second method

I'm going to discuss the parameters for this method.

1. old_string this parameter shows the old string that's a substring we've to remove.

2. second parameter, replace_string which you've to replace.

3. Third, to_replace_in shows the string in which we've to replace.

4. The last parameter is how many times we wants to replace the number of string occurence.

All of the above are parameters to replace the string.

Return value of it

Return value of string replacement is new_string with replaced value.

## When to use which method?

First of all, string replacement method is used in finding and handling large set of strings.

You can use first method if you're using Python 3.x version.

If you're using python 2.x then you can use second method where you've to import string module.

Although, second approach is little big.

I suggest you to use first method if you're using python 3.x versions.

Mostly, you'd like first method. This feature will help you in finding large set of strings and substrings.

Small conclusion

Finally, you've seen the two different approach for string replacement.

String replacement is great way where you don't have to replace the whole string.

You can replace only substring of string.

Also, you can change the occurence of the string.

So, what do you think about it? Comment on it !!

If you like please don't forget to share it.

Check out this articles

Also, you can connect with me at twitter.--->

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!