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
inkeyword to check if the word"Python"is in the variablesentenceand set it to the variablehas_python. - Use the
inkeyword to check if the word"hard"is in the variablesentenceand set it to the variablehas_hard.
Tests #
- Variable
has_pythonshould beTrue - Variable
has_hardshould beFalse
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules