Bird
Raised Fist0
Pythonprogramming~10 mins

String length and membership test in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - String length and membership test
Start with a string
Check length with len()
Test membership with 'in'
Use results in conditions or output
End
We start with a string, find its length using len(), then check if a character or substring is inside it using 'in'. Finally, we use these results to decide what to do.
Execution Sample
Python
s = "hello"
length = len(s)
check = 'e' in s
print(length)
print(check)
This code finds the length of the string 'hello' and checks if the letter 'e' is inside it, then prints both results.
Execution Table
StepVariableValueOperationResult/Output
1s"hello"Assign string"hello"
2lengthlen(s)Calculate length5
3check'e' in sMembership testTrue
4print(length)-Output length5
5print(check)-Output membershipTrue
6--End of program-
💡 Program ends after printing length and membership test results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
sundefined"hello""hello""hello""hello"
lengthundefinedundefined555
checkundefinedundefinedundefinedTrueTrue
Key Moments - 3 Insights
Why does len(s) return 5 for the string "hello"?
Because len() counts all characters in the string including letters, so 'h','e','l','l','o' total 5 characters as shown in step 2 of the execution_table.
What does the expression 'e' in s check?
It checks if the character 'e' exists anywhere inside the string s. Step 3 shows it returns True because 'e' is in "hello".
Why do we see True printed after the length?
Because print(check) outputs the result of the membership test which is True, as shown in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of length?
A4
B5
C6
Dundefined
💡 Hint
Check the 'Value' column for 'length' at step 2 in the execution_table.
At which step does the membership test 'e' in s happen?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the row where 'check' is assigned in the execution_table.
If we change s to "world", what would 'e' in s return?
AFalse
BError
CTrue
DNone
💡 Hint
Check the membership test logic in the execution_table and think if 'e' is in "world".
Concept Snapshot
String length and membership test in Python:
- Use len(string) to get number of characters.
- Use 'char' in string to check if character exists.
- len() returns an integer.
- 'in' returns True or False.
- Useful for conditions and outputs.
Full Transcript
This example shows how to find the length of a string and check if a character is inside it. We start by assigning the string 'hello' to variable s. Then we use len(s) to get the length, which is 5 because there are five letters. Next, we check if the letter 'e' is in s using 'e' in s, which returns True because 'e' is present. Finally, we print both results: 5 and True. This helps us understand how to measure string size and test membership in Python.

Practice

(1/5)
1.

What does the expression len('hello') return in Python?

easy
A. 5
B. 'hello'
C. 4
D. Error

Solution

  1. Step 1: Understand the len() function

    The len() function counts how many characters are in a string.
  2. Step 2: Count characters in 'hello'

    The word 'hello' has 5 letters: h, e, l, l, o.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    len('hello') = 5 [OK]
Hint: Count letters to find length quickly [OK]
Common Mistakes:
  • Counting letters incorrectly
  • Confusing len() with printing string
  • Expecting len() to return string itself
2.

Which of the following is the correct syntax to check if the letter 'a' is in the string word?

easy
A. word.contains('a')
B. word in 'a'
C. in 'a' word
D. 'a' in word

Solution

  1. Step 1: Recall Python membership syntax

    To check if a substring is inside a string, use substring in string.
  2. Step 2: Apply to letter 'a' and variable word

    We write 'a' in word to check if 'a' is inside word.
  3. Final Answer:

    'a' in word -> Option D
  4. Quick Check:

    Use 'in' as: substring in string [OK]
Hint: Use 'in' with substring first, then string [OK]
Common Mistakes:
  • Reversing order of 'in'
  • Using non-Python syntax like contains()
  • Syntax errors with 'in' placement
3.

What is the output of this code?

word = 'python'
print(len(word) > 5 and 'y' in word)
medium
A. False
B. 6
C. True
D. Error

Solution

  1. Step 1: Calculate len(word)

    The string 'python' has 6 characters, so len(word) is 6.
  2. 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.
  3. Final Answer:

    True -> Option C
  4. Quick Check:

    len(word)>5 and 'y' in word = True [OK]
Hint: Check length first, then membership with 'and' [OK]
Common Mistakes:
  • Confusing > with >= operator
  • Forgetting 'and' logic
  • Miscounting string length
4.

Find the error in this code snippet:

text = 'apple'
if 'p' not in text:
    print('No p found')
else
    print('p is present')
medium
A. Missing colon ':' after else
B. Wrong use of 'not in' operator
C. Variable 'text' is not defined
D. Indentation error on print statements

Solution

  1. Step 1: Check syntax of if-else

    Python requires a colon ':' after both if and else statements.
  2. Step 2: Identify missing colon

    The else line is missing a colon at the end, causing a syntax error.
  3. Final Answer:

    Missing colon ':' after else -> Option A
  4. Quick Check:

    else: needs colon [OK]
Hint: Always put ':' after else and if lines [OK]
Common Mistakes:
  • Omitting colon after else
  • Misusing 'not in' operator
  • Incorrect indentation
5.

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?

hard
A. [w for w in words if len(w) > 4 or 'e' in w]
B. [w for w in words if len(w) > 4 and 'e' in w]
C. [w for w in words if len(w) >= 4 and 'e' not in w]
D. [w for w in words if len(w) < 4 or 'e' not in w]

Solution

  1. Step 1: Understand the conditions

    We want words longer than 4 characters and that contain 'e'. This means length > 4 and 'e' in word.
  2. 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.
  3. Final Answer:

    [w for w in words if len(w) > 4 and 'e' in w] -> Option B
  4. Quick Check:

    Filter with len()>4 and 'e' in word [OK]
Hint: Use 'and' to combine length and membership tests [OK]
Common Mistakes:
  • Using 'or' instead of 'and'
  • Wrong comparison operators
  • Negating membership incorrectly