🔤 Working with Text, lesson 1 of 5
Strings and joining text
Combine strings with + and *, measure them with len(), convert with str() and map characters to numbers with ord() and chr().
8 min
1 exercise
3 quizzes
0/4 solved
Getting Python ready… examples can run in a moment.
A string (str) is a piece of text: letters,
digits, spaces, emoji... anything between quotes.
Strings can be combined:
+concatenates (joins) two strings*repeats a string a number of timeslen()counts the characters
Combining strings
+ doesn't add spaces for you. Put your own name in and count its letters.
What does this print?
You can't + a string and a number: Python won't
guess what you mean and raises a TypeError. Convert
the number to text first with str(), or pass both
to print separated by commas.
Mixing text and numbers
The first two lines work; the third raises a TypeError. Fix it with str(age).
Name badge
Build badge so it reads Hello, my name is Grace!
using the name variable and +.
Need text over several lines? Use triple quotes. Line breaks inside them are kept exactly as typed.
Multi-line strings
An emoji is a single character, so its length is 1.
What does this print?
ord() turns a one-character string into its number,
and chr() turns a number back into a character.
Capital letters have smaller numbers than lowercase ones
(A is 65, a is 97), so strings sort every capital
letter before every lowercase one.
Characters and numbers
ord("A") is 65 and chr(66) is "B", so chr(ord("A") + 2) is "C".
What does this print?