You are currently viewing Python String in operator with Examples

Python String IN operator is used to check if a substring is present in a string. In this article, we will discuss how to check if the substring exists in the string or not using in and not in operators in Python. The string is a set of characters similar to C, C++, and other programming languages.

Advertisements

1. Quick Examples of String in & not in operators


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"examples" present in string1 or not.
print("examples" in string1)

# Check whether the substring-"examples" present in string1 or not.
if("examples" in string1):
     print("Exists!")
else:
  print("Not exists!")

  # Check whether the substring-"examples" present in string1 or not.
print("examples" not in string1)

2. Python String in Operator

If you want to check whether a particular substring exists in the string or not use the Python string in operator. If the substring is present in the string, True is returned, otherwise False is returned. It is case-sensitive.

2.1 in Operator Syntax


# Here, substring is the string to be searched in the input string.
substring in string

2.2 Return

It will return a Boolean value.

  1. True – If the substring present in the string
  2. False – If the substring doesn’t present in the string.

2.3 Python String in Operator Examples


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"examples" present in string1 or not.
print("examples" in string1)

# Output:
# Actual String:  sparkby examples - Python tutorials
# True

Here, “examples” is the substring. True is returned since it is present in string1. Let’s see some examples that return False.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"hello" present in string1 or not.
print("hello" in string1)

# Output:
# Actual String:  sparkby examples - Python tutorials
# False

Here, “hello” is the substring. False is returned since it is not present in string1.

2.4 Using in Operator with if Statement

It can also be possible to use an in operator inside the if statement.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"examples" present in string1 or not.
if("examples" in string1):
     print("Exists!")
else:
  print("Not exists!")
# Output:
# Actual String:  sparkby examples - Python tutorials
# Exists!

So you can see we are checking whether the substring – “examples” present in the string or not using the in operator inside if statement. The statement inside if block is executed since the condition is true(“examples” present in the string).

3. Python String not in Operator

With Python String not in operator, If the substring is present in the string, False is returned, otherwise True is returned. It is case-sensitive. It is opposite to the Python String in operator.

3.1 not in Operator Syntax


# Here, substring is the string to be searched in the input string.
substring not in string

3.2 Return

It will return a Boolean value.

  1. True – If the substring not present in the string
  2. False – If the substring present in the string.

3.3 String not in Operator Examples


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"examples" present in string1 or not.
print("examples" not in string1)

# Output:
# Actual String:  sparkby examples - Python tutorials
# False

Here, “examples” is the substring. False is returned since it is present in string1. Following are examples that returns True.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"hello" present in string1 or not.
print("hello" not in string1)

# Output:
# Actual String:  sparkby examples - Python tutorials
# True

Here, “hello” is the substring. True is returned since it is not present in string1.

3.4 Using not in Operator with if Statement

It can also be possible to use Python string not in operator inside if statement.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("Actual String: ",string1)

# Check whether the substring-"examples" present in string1 or not.
if("examples" not in string1):
     print("Not Exists!")
else:
  print("exists!")
# Output:
# Actual String:  sparkby examples - Python tutorials
# exists!

So you can see we are checking whether the substring – “examples” present in the string or not using the not in operator inside if statement. The statement inside else block is executed since the condition is false inside the if block(“examples” present in the string).

4. Python string in vs not in Operator

in operatornot in operator
It will return True, if the substring is present in the string.It will return False, if the substring is present in the string.

5. Conclusion

In this article, we see how to check whether the given substring exists in the string or not using in and not in operators from Python. Both operators functionality is the same but the return value is different. It can be possible to use these operators with conditional statements.