Python · Variables and types
Strings
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Intro to Strings
Short intro
This lesson is a short introduction to strings. There is a chapter for them later in the course, but I wanted to introduce them here like all the other data types.
Strings are types of data in Python. They can be created using either single quotes (
' '
) or double quotes (
" "
). In it, you can basically write anything you want.
name = 'Aquachu' print(name) name = "Aquachu" print(name)
When do we use strings?
Strings are used to represent text data. They can be used for names, messages, or any other textual information. Strings are also used to format output, such as displaying messages to users.
Don't use string when not needed
The trap with strings is that you can use them to represent anything, but you should not. For example, if you want to represent a number, use an integer or a float. Don't create something like this:
level = '20'
because you will not be able to use it as a number.
Exercise 2 — Quotes inside Strings
Sometimes you want to use quotes inside your strings. But be careful - using the same quote type will break the string!
For example, saying
'Hello I'm here'
will break the string because the single quote in
I'm
ends the string early.
# ❌ This code will crash ! message = 'Hello I'm here' print(message)
The simple solution: Use different quote types!
If you want to say
'Hello I'm here'
without breaking the string, you can use double quotes:
"Hello I'm here"
.
# ✅ It works! message = "Hello I'm here" print(message) # ✅ It also works with single quotes inside double quotes! dialogue = 'She said "Hello"' print(dialogue)
The other solution: Escape characters
If the string contains both single and double quotes, you can use the backslash (\) to escape the quote that would otherwise break the string. For example,
'Hello I\'m here'
or
"She said \"Hello\""
.
Exercise 3 — Creating a string
Which string is correctly created?
Options
• ```message = "It's working!"```
• ```message = 'It's working!"```
• ```message = "It's working!'```
• ```message : "It's working!"```
Hint : Remember: Strings can be created using either single quotes ('') or double quotes (""). If you start creating a string with a single quote, you must end it with a single quote. If you start with a double quote, you must end it with a double quote.
Exercise 4 — Create two strings
For both variables, pick either single quotes (') or double quotes (") to create a string.
codemon_1 = ____Aqua"chu____ print(codemon_1) codemon_2 = ____Pyro'chu____ print(codemon_2)
Options: ' ' ' ' " " " "
Hint : Remember: You can't have a single quote inside a string that starts with a single quote. The same goes for double quotes. If you want to use both, you need to escape them using a backslash (\).
Exercise 5 — That's it for strings.. for now!
This chapter was a really short intro to strings. Now you know they exists and you know a new type of variable. But strings have a lot of uses. And for that reason, we will see them in detail in the next course!
Exercise 6 — What's your name?
Create a variable named
introduce_yourself
that contains a string introducing yourself. For example, you can say your name and your favorite programming language. Then print the variable.
Starter code
# Code here
Back to the course