Python · String in Depth
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Valid Codémon Name Check
Check if the Codémon's name is valid (more than 3 characters).
codemon_name = "Aquachu" is_valid = len(codemon_name) ____ 3 print(is_valid)
Options: > < >= ==
Hint : len() count the number of characters in a string. Count the characters and compare with 3.
Exercise 2 — Battle Banner Creation
Create a battle banner by repeating decorations and adding the title.
decoration = "=" title = "BATTLE" banner = decoration ____ 3 ____ " " ____ title ____ " " ____ decoration ____ 3 print(banner)
Options: * + * + + * + *
Hint : Use * to repeat strings and + to concatenate them.
Exercise 3 — Codémon Welcome Message
Create a welcome message using an f-string with the trainer's name and level.
trainer_name = "Pi" level = 1 message = ____"Welcome ____ to level ____!" print(message)
Options: f {trainer_name} {level} +
Hint : f-string format requires the prefix 'f' and curly braces for variables.
Exercise 4 — String Indexing
Match each string indexing expression on the left with the result on the right.
Items to match
• "Aquachu"[0]
• "Pyrochu"[-1]
• "Fire"[1:3]
• "Water"[:2]
Definitions
• "A"
• "u"
• "ir"
• "Wa"
Exercise 5 — Type Conversion and Addition
What is the final value of total_score in this code sequence?
score = '10' bonus = 5 total_score = int(score) + bonus print(total_score)
Options
• ```15```
• ```105```
• ```'105'```
• ```Error```
Hint : Think about type conversion and addition.
Exercise 6 — Type Conversion Result
What will be the result of:
str(3.14) + '0'
?
Options
• ```"3.140"```
• ```"3.14"```
• Error
• ```"3140"```
Hint : Think about string concatenation and type conversion.
Exercise 7 — String Slicing Result
What will be the result of:
"Florachu"[2:6]
?
Options
• ```"orac"```
• ```"lora"```
• ```"orach"```
• Error
Hint : Slicing starts at the first index and stops before the second index.
Exercise 8 — Advanced String Slicing
Match each slicing expression on the left with the result on the right.
Items to match
• "Python"[::2]
• "Hello"[::-1]
• "Codémon"[-3:]
• "Battle"[1:-1]
Definitions
• "Pto"
• "olleH"
• "mon"
• "attl"
Exercise 9 — String Concatenation Welcome
Create a welcome message using string concatenation.
trainer_name = "Pi" message = "Welcome, " ____ trainer_name ____ "!" print(message)
Options: + , and * + , and *
Hint : String concatenation is similar to math addition.
Exercise 10 — String Membership Check
What will
print("chu" in "Aquachu")
display?
Options
• ```True```
• ```False```
• Error
• ```"chu"```
Hint : The 'in' operator checks if a substring exists within a string.
Exercise 11 — String Slicing and Methods
Extract the first three characters.
codemon_type = "pyrochu" result = codemon_type________________ print(result)
Options: [ : 3 ]
Hint : Slicing uses square brackets and a colon to specify the range. The format is [start:end:step].
Exercise 12 — F-string Number Formatting
What will
print(f"{3.14159:.2f}")
display?
Options
• ```"3.14"```
• ```"3.142"```
• ```"3.1416"```
• ```"3"```
Hint : .Xf after a number means the number will be displayed with X digits after the decimal point.
Exercise 13 — Variable Types
Match variables on the left with their types on the right.
Items to match
• level = 42
• power = 3.14
• name = "Aquachu"
• rare = True
Definitions
• int
• float
• str
• bool
Exercise 14 — String Cleaning Result
What will be the final result after cleaning this messy name?
messy_name = " pYrOcHu " clean_name = messy_name.strip().lower().capitalize() print(clean_name)
Options
• ```"Pyrochu"```
• ```"pyrochu"```
• ```"PYROCHU"```
• ```" pYrOcHu "```
Hint : Follow each method step by step: strip removes spaces, lower makes lowercase, capitalize makes first letter uppercase.
Exercise 15 — String Multiplication
What happens when you multiply a string by a number in Python? Example:
"hi" * 3
Options
• ```"hihihi"```
• Error
• ```"hi3"```
• ```"3hi"```
Hint : Think about string repetition in Python.
Exercise 16 — Password Validation
Check if the password is long enough and contains the required character.
password = "securePW1" required_char = "1" is_valid = len(password) ____ 8 ____ required_char ____ password print(is_valid)
Options: >= and in == or replace find
Hint : The 'in' keyword allows you to check if a character is present in a string.
Exercise 17 — String Concatenation Result
What will be stored in message?
message = "Hello" + " " + "World"
Options
• ```"Hello World"```
• ```"HelloWorld"```
• ```"Hello World"```
• Error
Hint : String concatenation is similar to math addition.
Exercise 18 — Trainer Score Message
Create a message using an f-string.
trainer_name = "Bob" score = 95 message = ____"Trainer ____ has score ____" print(message)
Options: f {trainer_name} {score} trainer_name score + +
Hint : Remember to use f prefix and curly braces.
Exercise 19 — String Operations Results
Match the expression from the left with the result from the right.
Items to match
• "Hello" + "!"
• str(42)
• len("Pyrochu")
• "Hi" * 2
Definitions
• "Hello!"
• "42"
• 7
• "HiHi"
Exercise 20 — Type Compatibility Error
What happens when you try to add a number and a string?
score = 42 message = score + " points"
Options
• Error
• ```"42 points"```
• ```"42points"```
• Nothing
Hint : Think about type compatibility, can you add a number and a string?
Exercise 21 — Score Conversion Fix
Convert score so we can print the message without errors.
score = 100 message = "Score: " + ____(score) print(message)
Options: str int float bool
Hint : Remember to convert numbers before concatenating.
Exercise 22 — String Comparison Result
What is the result of:
"FIRE" == "fire".upper()
?
Options
• ```True```
• ```False```
• ```Error```
• ```"FIRE"```
Hint : The upper() method converts the string to uppercase before comparison.
Exercise 23 — Type Search in Name
Check if the type name is contained in the Codémon's full name.
codemon_name = "Pyrochu" type_name = "Pyro" has_type = type_name ____ codemon_name print(has_type)
Options: in == + is
Hint : The 'in' operator checks if one string is contained within another.
Exercise 24 — String Methods Results
Match each string method on the left with the result on the right.
Items to match
• "Hello".lower()
• "pyrochu".upper()
• " hi ".strip()
• "abc".replace('b', 'B')
Definitions
• "hello"
• "PYROCHU"
• "hi"
• "aBc"
Exercise 25 — Name Length Check
Check if the Codémon name has at least the minimum required length.
codemon_name = "Aquachu" min_length = 5 is_valid = len(codemon_name) ____ min_length print(is_valid)
Options: >= > == <
Hint : Having enough length means len() is greater than or equal to the minimum.
Back to the course