Python Basics · Getting Started · lesson 3 of 12
Working with strings
about 12 minutes · free · runs in your browser
Step 1 of 2
Joining and measuring text
Strings can be glued together with +. This is called concatenation:
first = "Ada"
last = "Lovelace"
full = first + " " + last # "Ada Lovelace"
Note the " " in the middle — Python joins exactly what you give it, so if you want a
space you have to supply one.
len() tells you how many characters a string contains, spaces included:
len("Ada Lovelace") # 12
Your turn: build full_name from first_name and last_name with a space
between them, then put its length in name_length.
You start from this, and edit it in the browser:
first_name = "Grace"
last_name = "Hopper"
# Build full_name and name_length below.
Step 2 of 2
f-strings
Gluing strings together with + gets unreadable quickly, and it breaks the moment one
of the pieces is a number. Python's answer is the f-string: put an f before the
opening quote, and anything inside {} is evaluated and dropped into the text.
name = "Ada"
age = 36
print(f"{name} is {age} years old")
That prints Ada is 36 years old. No +, no manual spaces, and the number needs no
conversion.
You can put any expression inside the braces, not just a name — f"{age + 1}" works.
Two string methods worth knowing now: .upper() and .lower() return a new string in
that case, leaving the original alone.
Your turn: build label so it reads PYTHON has 6 letters — the language name
uppercased, then its length.
You start from this, and edit it in the browser:
word = "Python"
# Build label with an f-string.