(Tutorial) Python String Contains – DataCamp


If you’re seeking to discover or change objects in a string, Python has a number of built-in strategies that may show you how to search a goal string for a specified substring.

.discover() Methodology


string.discover(substring, begin, finish)

Be aware: begin and finish are non-obligatory arguments.

From the above syntax, you may observe that the .discover() technique takes the specified substring because the necessary argument. You may specify the opposite two arguments: an inclusive beginning place and an unique ending place.

Within the instance code, you seek for Waldo within the string The place's Waldo?. The .discover() technique returns the bottom index within the string the place it may possibly discover the substring, on this case, eight.

my_string = "The place's Waldo?"

In the event you seek for Wenda, it returns -1 for the reason that substring shouldn’t be discovered.


Let’s have a look at if you could find Waldo between characters zero and 5. Within the code, you specify the beginning place zero and ending place as six, since this place shouldn’t be inclusive.

my_string = "The place's Waldo?"
my_string.discover("Waldo", 0, 6)

The .discover() technique doesn’t discover the substring and returns -1, as proven above.

.index() Methodology


string.index(substring, begin, finish)

Be aware: begin and finish are non-obligatory arguments.

From the above syntax, you may observe that the .index() technique takes the specified substring as a compulsory argument. It could actually take non-obligatory beginning and ending positions as nicely.

Within the instance, we search once more for Waldo utilizing .index().

my_string = "The place's Waldo?"

We get eight once more. Once we search for a substring that’s not there, we have now a distinction.

File "<stdin>", line 1, in <module>
ValueError: substring not discovered

The .index() technique raises an exception, as we will see within the output. We are able to deal with this utilizing the attempt besides block.

my_string = "The place's Waldo?"
besides ValueError:
      print("Not discovered")

Above, you may observe the syntax. The attempt half will take a look at the given code. If any error seems, the besides half will likely be executed, acquiring the next output because of this.

"Not discovered"

.rely() Methodology

The .rely() technique searches for a specified substring within the goal string. It returns the variety of non-overlapping occurrences. In easy phrases, what number of instances the substring is current within the string.


The syntax of .rely() is similar to the opposite strategies, as we will observe.

string.rely(substring, begin, finish)

Be aware: begin and finish are non-obligatory arguments.

Substring Rely

Within the instance, we use the .rely() technique to get what number of instances fruit seems.

my_string = "What number of fruits do you have got in your fruit basket?"

Within the output, we see that’s is 2.

We are able to then restrict the occurrences of fruit between character zero and fifteen of the string, as we will observe within the code beneath.

my_string.rely("fruit", 0, 16)

The tactic will return 1. Do not forget that the beginning place is inclusive, however the ending shouldn’t be.

.change Methodology

Generally you’ll want to change occurrences of a substring with a brand new substring. On this case, Python gives us with the .change technique.


string.change(previous, new, rely)

Be aware: rely is an non-obligatory argument.

As we see within the syntax above, it takes three arguments: the substring being changed, the brand new string to exchange it, and an non-obligatory quantity that signifies what number of occurrences to exchange.

Changing a Substring

Within the instance code, we change the substring home with automotive.

my_string = "The pink home is between the blue home and the previous home"
print(my_string.change("home", "automotive"))
The pink automotive is between the blue automotive and the previous automotive

The tactic will return a replica with all home substrings changed.

Changing a Particular Variety of Occurrences

On this instance, we specified that we solely need 2 of the occurrences to get replaced.

print(my_string.change("home", "automotive", 2))
The pink automotive is between the blue automotive and the previous home

Within the output, we see that the strategy returns a replica of the string with the primary two occurrences of home changed by automotive.

Interactive Instance

Within the beneath instance, you’ll:

  • Discover if the substring actor happens between the characters with index 37 and 41 inclusive. If it isn’t detected, print the assertion Phrase not discovered.
  • Change actor actor with the substring actor if actor happens solely two repeated instances.
  • Change actor actor actor with the substring actor if actor seems three repeated instances.
for film in films:
      # Discover if actor occurrs between 37 and 41 inclusive
    if film.discover("actor", 37, 42) == -1:
        print("Phrase not discovered")
    # Rely occurrences and change two by one
    elif film.rely("actor") == 2:  
        print(film.change("actor actor", "actor"))
        # Change three occurrences by one
        print(film.change("actor actor actor", "actor"))

Once we run the above code, it produces the next consequence:

Phrase not discovered
I imagine you I at all times mentioned that the actor is wonderful in each film he has performed
it is astonishing how scary the actor norton seems with a shaved head and a swastika on his chest.

Try it for yourself.

To be taught extra about discovering and changing strings, please see this video from our course, Regular Expressions in Python.

This content material is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.


Source link

Write a comment