AppDividend
Latest Code Tutorials

# Python String Contains | Python String __contains__()

Python String class has the __contains__() method that we can use to check if it contains another string or not. Methods that start with underscores are considered private methods in Python.  Some developers do not use these private methods in their code. They said that avoid using these private methods on production code.

## Python String __contains__() Example

Python string __contains__() is the instance method and returns the boolean value True or False depending on whether the string contains the specified string or not.

Note that the Python string __contains__() function is case sensitive. It returns false in the scenario like a character instead of a character.

### Syntax

`string.__contains__(substr)`

Let’s look at the below example.

```# app.py

data = 'AppDividend'
print(data.__contains__('Div'))
```

Now, let’s see the output.

In the above code, substring Div is inside the data string. That is why it returns True.

See the below more examples.

Now, __contains__() function is a case sensitive. So, if you put the character app instead of App in the above program, then it will return false.

See the below code.

```# app.py

data = 'AppDividend'
print('AppDividend contains app =', data.__contains__('app'))```

See the below output.

Python String Contains method checks the substring, which includes in the string. If it includes then return True otherwise False.

Let’s look at the second example in which we will ask a user to enter both the input strings and check if the first string contains the second string or not.

```# app.py

strA = input('Please enter first input string\n')
strB = input('Please enter second input string\n')

print('First Input String Contains Second String? ', strA.__contains__(strB))```

See the following output.

If you also need the first index of the substring, you can use find(substr) to find the index.

If Python String find() method returns -1, it means that substring doesn’t exist in the string.

Python String index() method returns the index of a substring inside the string if found.

So, you can use the above two methods instead of the __contains__() method to check the substring inside the string.

Finally, Python String __contains__() example article is over.

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6941063,"math_prob":0.6488147,"size":2245,"snap":"2020-24-2020-29","text_gpt3_token_len":468,"char_repetition_ratio":0.19589469,"word_repetition_ratio":0.0057142857,"special_character_ratio":0.2311804,"punctuation_ratio":0.10869565,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9596181,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-04T01:28:01Z\",\"WARC-Record-ID\":\"<urn:uuid:4e257825-3657-4ace-a9f3-a55089379e3b>\",\"Content-Length\":\"117011\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2133783a-553e-494d-8f83-34f2cc62e2ca>\",\"WARC-Concurrent-To\":\"<urn:uuid:110bce25-b160-482f-a6d4-42f029feab64>\",\"WARC-IP-Address\":\"52.86.133.10\",\"WARC-Target-URI\":\"https://appdividend.com/2019/05/25/python-string-contains-tutorial-example-python-string-__contains__/\",\"WARC-Payload-Digest\":\"sha1:SIZBPBZTVLMY5G6OQN3JVW7PVKHT4SEW\",\"WARC-Block-Digest\":\"sha1:VEUWSU2AZGXEEQP23JHKCGQAGNPA766N\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655883961.50_warc_CC-MAIN-20200704011041-20200704041041-00039.warc.gz\"}"}