Does Python have a string \'contains\' substring method?



In Python, a substring is a sequence of characters that appears within a larger string. For example, in the string "Hello, world!", the substring "world" appears within the larger string.

A substring can be a single character, or it can be a longer sequence of characters. Substrings are useful when you need to extract or manipulate specific parts of a string. You can also check if a substring is contained within a larger string using Python's string methods.

Python provides a built-in method to check if a string contains a substring. The method is called ?in' and it returns a Boolean Value True if the substring is found in the string, and False otherwise.

Here are some examples of how to use the in method to check if a string contains a substring:

Example

In this example, we define a string "Lorem Ipsum" and check if it contains the substring "Lorem". Since the substring is found, the program prints "Substring found!".

# Define a string text = "Lorem Ipsum" # Check if the string contains a substring if "Lorem" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring found! 

Example

In this example, we define a string "This is a test string" and check if it contains the substring "test". Since the substring is found, the program prints "Substring found!".

# Define a string text = "This is a test string" # Check if the string contains a substring if "test" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring found! 

Example

In this example, we define a string "Python is a great coding language" and check if it contains the substring "Javascript". Since the substring is not found, the program prints "Substring not found."

# Define a string text = "Python is a great coding language" # Check if the string contains a substring if "Javascript" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring not found. 

Example

In this example, we define a string "The wild horse jumps over the fence" and check if it contains the substring "black". Since the substring is found, the program prints "Substring not found.".

# Define a string text = "The wild horse jumps over the fence" # Check if the string contains a substring if "black" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring not found 

Example

In this example, we define a string "I love to code in Python" and check if it contains the substring "code". Since the substring is found, the program prints "Substring found!".

# Define a string text = "I love to code in Python" # Check if the string contains a substring if "code" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring found! 

Example

In this example, we define a string "Python is an interpreted high-level programming language" and check if it contains the substring "interpreted". Since the substring is found, the program prints "Substring found!".

# Define a string text = "Python is an interpreted high-level programming language" # Check if the string contains a substring if "interpreted" in text: print("Substring found!") else: print("Substring not found.") 

Output

Substring found! 
Updated on: 2023-08-10T12:02:36+05:30

692 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements