How To: Strings and Things - Words and Letters


Strings

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.

Turning text into strings

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.

ParseErrors 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 NameErrors when we talk about variable containers, but for now, know that Python's confused when you type words without surrounding quotes.

Challenge

Make a few strings of your own.

Multi-line strings

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.

Triple quotes to create strings

How can you make a multi-line string? Surround your words with three sets of double quotes:

"""Hi there!
I have 
multiple
lines.
:)"""

Two sets of double quotes?

Surrounding strings with two sets of quotes doesn't do anything. And remember that using one set of double quotes creates a "normal" string.

Challenge

  1. Create a string that includes a linebreak
  2. Create a string that doesn't have a linebreak

These strings can include any text you like.

Adding and multiplying strings

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.

Adding strings together

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!)

Subtraction?

Nah. Subtracting strings doesn't work.

Multiplying strings by numbers

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?

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.)

Challenge

  1. Use string multiplication to say "hi!" ten times
  2. Use string addition to wish goodbye to this section; you'll want to write a line of code that'll print something like "Goodbye string addition and multiplication!"