0
0
Pythonprogramming~10 mins

String validation checks 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 check if the string contains only digits.

Python
text = "12345"
result = text.[1]()
print(result)
Drag options to blanks, or click blank then click option'
Aisalpha
Bislower
Cisspace
Disdigit
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using isalpha() which checks for letters, not digits.
Using isspace() which checks for spaces.
2fill in blank
medium

Complete the code to check if the string contains only alphabetic characters.

Python
word = "Hello"
if word.[1]():
    print("All letters")
else:
    print("Contains other characters")
Drag options to blanks, or click blank then click option'
Aisdigit
Bisalnum
Cisalpha
Disspace
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using isdigit() which checks for digits.
Using isalnum() which allows letters and digits.
3fill in blank
hard

Fix the error in the code to check if the string contains only whitespace characters.

Python
space_str = "   \t\n"
print(space_str.[1]())
Drag options to blanks, or click blank then click option'
Aisspace
Bisalpha
Cisdigit
Disalnum
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using isalpha() which checks for letters.
Using isdigit() which checks for digits.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 characters.

Python
words = ["apple", "cat", "banana", "dog"]
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 'word' instead of 'len(word)' for values.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words with length greater than 4.

Python
words = ["tree", "flower", "grass", "bush"]
result = { [1]: [2] for w in words if len(w) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'w' instead of 'w.upper()' for keys.
Using '<' instead of '>' for filtering.