Master Python String Contains w3schools Guide

Logan Jul 13, 2026

W3Schools, a popular web development learning platform, offers comprehensive tutorials and reference materials for Python programming. Among its extensive range of topics, strings are a fundamental aspect that every Python learner should master. One of the most common operations on strings is checking if a string contains a certain substring. Let's explore how to do this using Python and the methods provided by W3Schools tutorials.

Python string slicing cheat sheet
Python string slicing cheat sheet

Before we dive into the 'contains' method, let's recall that in Python, strings are immutable, meaning we cannot change their values after creation. However, we can manipulate them in various ways, and one of these ways is to check if one string is present within another.

W3Schools.com
W3Schools.com

The 'in' Operator

The most straightforward way to check if a string contains another string is by using the 'in' operator. This operator returns 'True' if the first string (search string) is found within the second string (main string), and 'False' otherwise.

10 Python String Methods Every Beginner Should Know
10 Python String Methods Every Beginner Should Know

Here's a simple example taken from the W3Schools Python tutorial:

```python txt = "Hello, welcome to my world." x = "my" in txt print(x) ```

When you run this code, it will output 'True', indicating that the word 'my' is indeed present in the string 'txt'.

Master Python Strings: Essential Concepts Every Beginner Should Learn
Master Python Strings: Essential Concepts Every Beginner Should Learn

Case Sensitive Search

The 'in' operator is case sensitive. This means it distinguishes between uppercase and lowercase letters. So, if you're searching for 'My', it won't find 'my' or 'MY'. Here's an example:

```python txt = "Hello, welcome to my world." x = "My" in txt print(x) ```

Running this code will output 'False', even though 'my' is present in the string. To make the search case insensitive, you can convert both the search string and the main string to the same case using the 'lower()' or 'upper()' method.

string methods in python
string methods in python

Sensitive to Levenshtein Distance

The 'in' operator is not sensitive to Levenshtein distance, meaning it won't find substrings that are 'close' to the search string. For example, it won't find 'monk' in 'month', even though they have a small Levenshtein distance (only one character difference).

The 'find()' Method

How to Use the String join() Method in Python
How to Use the String join() Method in Python

Another way to check if a string contains another string is by using the 'find()' method. This method searches for the first occurrence of the search string in the main string and returns its index if found. If the search string is not found, it returns -1.

The 'find()' method is also case sensitive and not sensitive to Levenshtein distance, similar to the 'in' operator. Here's an example from W3Schools:

How to Lowercase Python String with Example Programs
How to Lowercase Python String with Example Programs
Python String endswith() Method with Examples
Python String endswith() Method with Examples
Python strings cheat sheet
Python strings cheat sheet
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
a computer screen with the words python string method
a computer screen with the words python string method
a blackboard with an image of a person and the words string operators in python
a blackboard with an image of a person and the words string operators in python
Python String Methods Cheat Sheet for Beginners | Learn upper(), lower(), split(), replace() & More
Python String Methods Cheat Sheet for Beginners | Learn upper(), lower(), split(), replace() & More
Python String Methods Cheat Sheet 🐍
Python String Methods Cheat Sheet 🐍
a poster with the words string method in python
a poster with the words string method in python
💻 курсы по it в тг - перейти
💻 курсы по it в тг - перейти
Converting an Integer to a String in Python
Converting an Integer to a String in Python
Python String Methods — Small Functions, Big Power
Python String Methods — Small Functions, Big Power
String Concatenation in Python – Easy & Quick!
String Concatenation in Python – Easy & Quick!
Top Open Source Tools to Explore
Top Open Source Tools to Explore
Python String Length
Python String Length
String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Python Programming tips and tricks
Python Programming tips and tricks
a screenshot of what are f - strings in python text on a black background
a screenshot of what are f - strings in python text on a black background
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
How to Compare Strings in Python (Python Tutorial)
How to Compare Strings in Python (Python Tutorial)

```python txt = "Hello, welcome to my world." x = txt.find("my") print(x) ```

Running this code will output '12', which is the index of the first occurrence of 'my' in the string 'txt'. If the string is not found, the output will be '-1'.

Finding the Last Occurrence

The 'rfind()' method works similarly to 'find()' but searches for the last occurrence of the search string rather than the first one. Here's an example:

```python txt = "Hello, welcome to my world. I love my Python programming world." x = txt.rfind("my") print(x) ```

Running this code will output '33', which is the index of the last occurrence of 'my' in the string 'txt'. If the string is not found, the output will still be '-1'.

In conclusion, the 'in' operator and the 'find()' method are powerful tools for checks if a string contains another string in Python. They can be used for simple checks or within more complex algorithms. W3Schools provides a wealth of examples and exercises to help you master these concepts and more. Now, go ahead and practice these methods to deepen your understanding and become a more proficient Python programmer.