Code Bytes · "Intro to Programming" · Episode 6 · Data
Strings
Text in code is called a string because it is a string of characters, lined up in order like beads on a string. In Python you make one with quotes, + joins strings together, and len() counts the characters, spaces and commas included.
A string of characters
Why is text in code called a string? Because it is a string of characters: letters, digits, spaces and symbols, lined up in a fixed order, like beads on a thread. The text Hi, Sam is a row of beads: H, i, a comma, a space, S, a, m. The space is a character too, just an invisible one.
Make a string with quotes
In Python, you make a string by putting text in quotes. Double quotes and single quotes both work, and they make exactly the same string:
``python print("Hi" == 'Hi') ``
`` True ``
The quotes are not part of the text. They only mark where the string starts and where it ends, so print("Hi") shows Hi, with no quotes.
Put a string in a box
A variable is a labelled box for a value. Here the box is called name and the string "Sam" goes inside:
``python name = "Sam" ``
Join strings with +
With strings, + doesn't add numbers. It joins strings together, end to end (programmers call this concatenation):
``python name = "Sam" greeting = "Hi, " + name print(greeting) ``
`` Hi, Sam ``
Look closely at "Hi, ": the comma and the space are inside the quotes, so they are part of the string. That's why the output has them. Leave the space out and you would get Hi,Sam.
Count characters with len()
len, short for length, counts the characters in a string:
``python name = "Sam" print(len(name)) ``
`` 3 ``
Every character counts, even spaces and commas. A space looks like nothing, but to Python it is a character just like a letter.
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python first = "Byte" last = "Sized" print(first + " " + last) print(len(first + last)) print(len("")) ``
You should see:
`` Byte Sized 9 0 ``
The " " in the middle is a string holding just one space; without it the words would run together. first + last is ByteSized, nine characters. And "" is the empty string: quotes with nothing between them, length 0. Now change last to your own name and predict the length before you run it.
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- Python's name for the string type is
str. You'll see it in error messages. +only joins a string to another string."Age: " + 5stops with aTypeError; turn the number into text first withstr(5), so"Age: " + str(5)givesAge: 5.len()really counts Unicode code points. For plain letters, digits, spaces and punctuation that's the same as characters, but a few emoji or accented letters can count as more than one.- The beads picture is our own way to show it. The word "string" comes from older mathematics and early computing, where a string meant a sequence of symbols.
Answer to the comment question
What does print(len("cat dog")) show? 7. cat is 3 characters, dog is 3 more, and the space between them counts as a character too: 3 + 1 + 3 = 7. (The quotes don't count, because they aren't part of the text.)
Test your knowledge
Quick quiz: Strings
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "An Informal Introduction to Python": Text: single or double quotes, + joins strings, len() gives the length
- Python Software Foundation, "Built-in Types": Text Sequence Type str: strings are sequences of characters (Unicode code points)
- Python Software Foundation, len() built-in function
- Python Software Foundation, "String and Bytes literals": quote styles
Video transcript
Why is text in code called a string?
Because it's a string of characters: letters, digits, spaces and symbols, lined up in order like beads on a string.
In Python, you make a string with quotes. Double quotes or single quotes both work. The quotes aren't part of the text. They just mark where it starts and ends.
Let's put a name in a box. We type name equals Sam, in quotes.
Plus joins strings together. Greeting equals Hi with a comma and a space, plus name. Print it, and you get Hi, Sam.
And len, short for length, counts the characters. Len of name gives three. Every character counts, even spaces and commas.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP3Your first line of code
- "Intro to Programming" EP4Variables
- "Intro to Programming" EP8Comparison and logic
- "Intro to Programming" EP9For loops