0
0
Pythonprogramming~10 mins

String length and membership test in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the length of the string.

Python
word = "hello"
print(len([1]))
Drag options to blanks, or click blank then click option'
Aword
B"word"
Clen(word)
Dhello
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around the variable name like "word" which is a string literal, not the variable.
Using len(word) inside len() causing an error.
2fill in blank
medium

Complete the code to check if the letter 'a' is in the string.

Python
text = "apple"
if [1] in text:
    print("Found a")
Drag options to blanks, or click blank then click option'
A"a"
Ba
C'text'
D"text"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the variable name without quotes, which causes a NameError.
Using quotes around the variable name instead of the letter.
3fill in blank
hard

Fix the error in the code to check if 'x' is not in the string.

Python
my_str = "example"
if 'x' [1] my_str:
    print("No x found")
Drag options to blanks, or click blank then click option'
Ain
Bnot in
Cnot
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using only not without in.
Using != which compares equality, not membership.
4fill in blank
hard

Fill both blanks to create a dictionary of words with length greater than 3.

Python
words = ["cat", "lion", "dog", "tiger"]
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself instead of its length.
Using less than operator which filters wrong words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values longer than 4.

Python
words = ["apple", "pear", "banana", "kiwi"]
result = [1]: [2] for w in words if len(w) [3] 4}
Drag options to blanks, or click blank then click option'
Aw.upper()
Bw
C>
Dlen(w)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using length as value instead of the word.
Using less than operator which filters wrong words.