0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
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.