How Can We Help?
In this tutorial, We are going to explain how to use Python string membership, logical operators, basic syntax, and many examples for better understanding.
What are the membership operators for string? There are two membership logical operators in Python which are (in) & (not in). They are used to check whether the given element is a part of the given string or not.
An “in” operator keyword returns True. If the given element / entire substring is present in the given string, otherwise it returns False.
A “not in” operator keyword returns True. If the given element / entire substring is not present in the given string, otherwise it returns False.
Syntax:
<string_element> in <string_variable_name>
OR
<string_element> not in <string_variable_name>
Input Parameters:
- string_element : The string that we want to be check whether it’s a part from other string or not.
- string_element : Is the original string variable that we need to check a substring inside.
- in & not in : Is the logical membership operators
Example (1)
#check prsence of substring variable
str1='python script'
str2='script'
print('str2 is a part of str1:',str2 in str1)
Output of example 1
str2 is a part of str1: True
In the above example, using membership operator (in), we have checked whether string variable ‘str2’ is a part of the variable ‘str1’ and get the output as True because value ‘script’ is a substring of ‘str1’.
Example (2)
#check presence of substring
print('py is a part of python:','py' in 'python')
Output of example 2
py is a part of python: True
In the above example, using membership operator (in), we have checked whether string value ‘py’ is a part of the string value ‘python’ and get the output as True because value ‘py’ is a substring of ‘python’.
Example (3)
#check presence of substring
print('java is not a part of python:','java' not in 'python')
Output of example 3
java is not a part of python: True
In the above example, using membership operator ( not in), we have checked whether string value ‘java’ is a part of the string value ‘python’ and get the output as True because value ‘java’ is not a substring of ‘python’.
Note: If we try to check integer or float value presence in string then it will give a TypeError.
Example (4)
#check presence of substring
print('py is a part of python:',3.8 in 'python3.8')
Output of example 4
Traceback (most recent call last):
File “C:/Users/Desktop/exam.py”, line 1, in
print(‘py is a part of python:’,3.8 in ‘python3.8’)
TypeError: ‘in ‘ requires string as left operand, not float
In the above example, we tried to check the presence of float value ‘3.8’ in string value ‘python3.8’ and after executing the code, we got the error message because membership operators were only allowed to check the presence of the same data type values.
In this tutorial, you have learned What is a string membership operators in Python.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in comment. we are waiting your feedback as well.
Similar Python operators:
- Concatenation(‘+’) : It adds two string.
- string Iteration : It returns element of string by using for loop with string.
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.