Provided a better code. (#33478)
* Provided a better code. Updated the code to give desired output. * fix: corrected some verbiage
This commit is contained in:
committed by
Randell Dawson
parent
c094443cd7
commit
7b022e2d72
@ -15,10 +15,27 @@ newString = string.replace("is","WAS")
|
|||||||
print(newString)
|
print(newString)
|
||||||
```
|
```
|
||||||
|
|
||||||
Output
|
Output:
|
||||||
```python
|
|
||||||
|
```shell
|
||||||
ThWAS WAS nice. ThWAS WAS good.
|
ThWAS WAS nice. ThWAS WAS good.
|
||||||
```
|
```
|
||||||
|
As you can see above, the "is" in This is also replaced with Was.
|
||||||
|
|
||||||
|
To prevent this we can use <br>
|
||||||
|
|
||||||
|
```python
|
||||||
|
string = "This is nice. This is good."
|
||||||
|
newString = string.replace(" is "," WAS ")
|
||||||
|
print(newString)
|
||||||
|
```
|
||||||
|
|
||||||
|
Now the output becomes:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
This WAS nice. This WAS good.
|
||||||
|
```
|
||||||
|
Here the "is" between whitespaces gets changed to "Was"
|
||||||
|
|
||||||
2. Replace the first 2 occurrences of `"is"` with `"WAS"`
|
2. Replace the first 2 occurrences of `"is"` with `"WAS"`
|
||||||
|
|
||||||
@ -28,8 +45,9 @@ newString = string.replace("is","WAS", 2)
|
|||||||
print(newString)
|
print(newString)
|
||||||
```
|
```
|
||||||
|
|
||||||
Output
|
Output:
|
||||||
```python
|
|
||||||
|
```shell
|
||||||
ThWAS WAS nice. This is good.
|
ThWAS WAS nice. This is good.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user