We use string length to know how many characters are in a word or sentence. Membership test helps us check if a letter or word is inside another word or sentence.
String length and membership test in Python
Start learning this pattern below
Jump into concepts and practice - no test required
len(string) substring in string
len(string) gives the number of characters in the string.
substring in string checks if substring is inside string and returns True or False.
len("hello") # returns 5
"a" in "apple" # returns True
"z" in "apple" # returns False
len("") # returns 0
This program counts letters in the word "banana" and checks if letters "a" and "z" are inside it.
word = "banana" print(f"The word '{word}' has {len(word)} letters.") letter = "a" if letter in word: print(f"The letter '{letter}' is in the word.") else: print(f"The letter '{letter}' is not in the word.") letter = "z" if letter in word: print(f"The letter '{letter}' is in the word.") else: print(f"The letter '{letter}' is not in the word.")
Spaces and punctuation count as characters in length.
Membership test is case sensitive: 'A' and 'a' are different.
Use len() to find how many characters are in a string.
Use in to check if a smaller string or letter is inside a bigger string.
These tools help us understand and check text easily.
Practice
What does the expression len('hello') return in Python?
Solution
Step 1: Understand the len() function
Thelen()function counts how many characters are in a string.Step 2: Count characters in 'hello'
The word 'hello' has 5 letters: h, e, l, l, o.Final Answer:
5 -> Option AQuick Check:
len('hello') = 5 [OK]
- Counting letters incorrectly
- Confusing len() with printing string
- Expecting len() to return string itself
Which of the following is the correct syntax to check if the letter 'a' is in the string word?
Solution
Step 1: Recall Python membership syntax
To check if a substring is inside a string, usesubstring in string.Step 2: Apply to letter 'a' and variable word
We write'a' in wordto check if 'a' is inside word.Final Answer:
'a' in word -> Option DQuick Check:
Use 'in' as: substring in string [OK]
- Reversing order of 'in'
- Using non-Python syntax like contains()
- Syntax errors with 'in' placement
What is the output of this code?
word = 'python' print(len(word) > 5 and 'y' in word)
Solution
Step 1: Calculate len(word)
The string 'python' has 6 characters, solen(word)is 6.Step 2: Evaluate conditions
Check if 6 > 5 (True) and if 'y' is in 'python' (True). Both are True, so the whole expression is True.Final Answer:
True -> Option CQuick Check:
len(word)>5 and 'y' in word = True [OK]
- Confusing > with >= operator
- Forgetting 'and' logic
- Miscounting string length
Find the error in this code snippet:
text = 'apple'
if 'p' not in text:
print('No p found')
else
print('p is present')Solution
Step 1: Check syntax of if-else
Python requires a colon ':' after both if and else statements.Step 2: Identify missing colon
The else line is missing a colon at the end, causing a syntax error.Final Answer:
Missing colon ':' after else -> Option AQuick Check:
else: needs colon [OK]
- Omitting colon after else
- Misusing 'not in' operator
- Incorrect indentation
Given a list of words, write a Python expression to create a new list containing only words longer than 4 characters and containing the letter 'e'.
Which of these is correct?
Solution
Step 1: Understand the conditions
We want words longer than 4 characters and that contain 'e'. This means length > 4 and 'e' in word.Step 2: Check list comprehension syntax
The correct syntax is[w for w in words if len(w) > 4 and 'e' in w]which filters words by both conditions.Final Answer:
[w for w in words if len(w) > 4 and 'e' in w] -> Option BQuick Check:
Filter with len()>4 and 'e' in word [OK]
- Using 'or' instead of 'and'
- Wrong comparison operators
- Negating membership incorrectly
