Editor settings

General settings

Save settings

Learn to program with Python

Use in keyword with strings #

The in keyword in Python is used to check if a value exists in a sequence, such as a string, list, or dictionary. The general syntax for using in is:

value in sequence

Here, value is the value that you want to check if it exists in the sequence. If the value exists in the sequence, the in operator will return True, otherwise, it will return False

The in keyword is used to check if a substring or character exists within a string. It returns True if the substring or character is found in the string, and False if it is not found.

For example:

string = "Hello, World!"
print('H' in string) # Output: True
print('h' in string) # Output: False
print('World' in string) # Output: True
print('world' in string) # Output: False

Exercise #

  • Use the in keyword to check if the word "Python" is in the variable sentence and set it to the variable has_python.
  • Use the in keyword to check if the word "hard" is in the variable sentence and set it to the variable has_hard.

Tests #

  • Variable has_python should be True
  • Variable has_hard should be False
Output will be displayed here