Python · String in Depth
Concat and repeat
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What is a String?
I already introduced you to strings so it's normal if you have a déjà-vu. In the next chapters, we will explore them in depth.
A string is a sequence of characters. In Python, strings are created by enclosing characters in quotes.
name = "Aquachu"
print(name)
message = 'Hello, trainer!'
print(message)
You can use either single quotes (') or double quotes (") to create a string in Python.
Exercise 2 — Creating Strings
Which of the following is a valid way to create a string in Python?
Options
• ```name = "Aquachu"```
• ```name = (Aquachu)```
• ```name = Aquachu```
• ```name = /Aquachu/```
Hint : Strings in Python must be enclosed in quotes (either single or double quotes).
Exercise 3 — Special Characters in Strings
Python allows you use special characters in strings, such as newlines, tabs, quote. Usually, the backslash (\) is used to do this.
- Newline:
\n
- Tab:
\t
- Single quote:
\'
- Double quote:
\"
print('Hello,\nTrainer!')
print('\tMy name is \'Pierre\'!')
Exercise 4 — Creating String Variables
Fill in the blanks to create valid string variables:
codemon_name = ____Aquachu____
print(codemon_name)
greeting = ____'Hello, trainer!'____
print(greeting)
Options: " " ' ' ' '
Hint : In Python, you can create strings using either single quotes (') or double quotes ("). Both work the same way, as long as you use the same type of quotes to open and close the string.
Exercise 5 — String Concatenation
Like numbers, strings can use operators. This is pretty useful in some situations like wanting to combine two strings together or displaying the same character multiple times.
Let's start with concatenation. It means joining strings together. In Python, you can use the + operator to concatenate strings.
first_name = "Aqua"
last_name = "chu"
full_name = first_name + last_name
print(full_name)
strings and numbers are different
Remember: The + operator works differently for strings and numbers. With strings, it concatenates them. With numbers, it adds them.
Exercise 6 — String Concatenation Result
What will be the output of the following code?
name = "Flora" + "chu"
power = "leaf"
print(name + " has " + power + " power!")
Options
• Florachu has leaf power!
• Flora + chu has leaf power!
• Florachu has leafpower!
• Error: cannot concatenate strings and variables
Hint : The code concatenates the strings "Flora" and "chu" to form "Florachu", then concatenates this with the strings " has ", the value of the power variable ("leaf"), and the string " power!" to produce the final output.
Exercise 7 — String Repetition
In Python, you can repeat a string using the * operator followed by the number of repetitions.
star = "*"
print(star * 10)
codemon_call = "Pyrochu! "
print(codemon_call * 3)
pattern = "-="
print(pattern * 5)
String repetition is useful for creating patterns, borders, or repeating text elements in your program.
Exercise 8 — String Operations and Results
Match each string operation with its correct result:
Items to match
• "Aqua" + "chu"
• "Go! " * 3
• "1" + " " + "2"
• "=" * 10
Definitions
• "Aquachu"
• "Go! Go! Go! "
• "1 2"
• "=========="
Exercise 9 — Codémon Lineup
Join 3 Codémon names and repeat a cheer.
a = "Aquachu"
b = "Pyrochu"
c = "Florachu"
d = a ____ b ____ c
print(d)
cheer = "Go! "
e = cheer ____ 3
print(e)
Options: + * + * + *
Hint : `+` joins strings, `*` repeats them.
Exercise 10 — Combining Concatenation and Repetition
What is the output of the following code ?
border = '=' * 5
title = ' Codémon Battle '
header = border + title + border
print(header)
Options
• ```===== Codémon Battle =====```
• ```=====Codémon Battle=====```
• ```===== Codémon Battle=====```
• ```= = = = = Codémon Battle = = = = =```
Hint : The code first creates a border of 5 equal signs using repetition. Then it concatenates the border, the title with spaces, and another border to create the full header.
Exercise 11 — Codémon Battle Announcement
Create a battle announcement for a Codémon match! Use string concatenation and repetition to build an impressive banner. Your announcement should include:
- A decorative border using repetition
- The names of two Codémon battling (like Aquachu vs Pyrochu)
- A battle message
- Another decorative border
Starter code
# Create your battle announcement here
Back to the course