Computer scientists have a special word for text: strings
. It's an old term – dating back to printing presses in the late 1890s! – that came about because all words are made up of letters that have been "strung together."
Anything can be a string when programming, just like anything can become text when you're typing.
To make a string, surround your text (word, numbers, symbols, or a mix of all three) with either single (') or double (") quotes.
Here's a few examples:
If you don't surround your word by double quotes or single quotes – say if you use double quotes on the left and single ones on the left, or you forget quotes on one side – you'll get a ParseError
.
ParseError
s are Python's way of saying, "Hey! I was trying to read the word you were writing, but I got confused."
Leaving off the quotes entirely won't work either; you'll get a NameError
. We'll talk more about NameError
s when we talk about variable containers, but for now, know that Python's confused when you type words without surrounding quotes.
Make a few strings of your own.
When you make a Python string with double quotes (""
), you can't use line breaks (spaces in between lines.)
If you try, Python will send you a ParseError
, which is a way of saying "Hey! I tried to read – to parse – the words you gave me, but I got confused and couldn't tell what you meant."
This is because computers assign special meaning to line breaks. They do this for reasons that used to be important when computers were less powerful.
It's only the line break that Python's allergic to; it's just fine if you've so much text that it spills on to another line:
See? All that overflowing text became a string; it just become one without line breaks.
How can you make a multi-line string? Surround your words with three sets of double quotes:
"""Hi there!
I have
multiple
lines.
:)"""
Surrounding strings with two sets of quotes doesn't do anything. And remember that using one set of double quotes creates a "normal" string.
These strings can include any text you like.
There's many ways to manipulate strings, and we'll talk about some of them in a future lesson.
For now, addition and multiplication are two neat things you can do with strings.
Python allows you to add strings together:
In Python's world, adding strings glues them together, no matter what the strings' value. Notice that if you want spaces in between the different string-words, you've got to add them yourself; Python doesn't assume each string you're adding is its own word.
Here's another example of Python's way of "gluing" strings together:
This can be confusing, because 1 + 7 = 8
, but '1' + '7' = '17'
. (Adding together string-numbers, rather than number-numbers, ends up being a common source of bugs in programs!)
Nah. Subtracting strings doesn't work.
Python also allows you to multiply strings by numbers:
Even if the strings are stringified numbers, Python still just duplicates them – when we're dealing with strings, no math is preformed.
"7" * 3 # Produces "777"
"3" * 10 # Produces "3333333333"
Division with strings doesn't work, either. (Which is kindof a relief, since it's hard to think about what it would mean to divide a string.)
"Goodbye string addition and multiplication!"