Strings and Things

REPL

As we cover code together in this workshop, a lot of the code will be typed at the Python REPL.

What is the REPL? REPL stands for Read-Execute-Print Loop. Don’t try to remember that because it doesn’t matter. You can call it Reptile-Eats-Pizza Loop and it wouldn’t change the way you code. Don’t stress.

It is important to know what the REPL does. It:

  1. Reads each character you type
  2. Executes the code you type
  3. Prints out the resulting response, if any
  4. Loops to start over and waits for you to keep typing

To start the REPL, open your command/terminal window and type:

$ python3.6

Or on Windows:

$ py -3.6

This will start the REPL. You can tell when you are in the REPL because the prompt will change to >>>. Look! It’s like a little hungry reptile!

Exit the REPL with ^Z  <Enter> (ctrl-Z followed by <Enter>) on Windows, or ^D (ctrl-D) on OS X or Linux. Note for OS X, it’s not cmd-D.

I tend to forget the quick keys for that, so I often type this:

>>> exit()

Either works.

Strings

Okay, now that we’re using our Reptile-Eats-Pizza Loop, let’s play!

First up: strings. Strings allow you to store text. Now, nearly every time you play with a new language or framework the very first thing you do is the same: get the words “Hello world!” to show up somewhere. That’s fine and dandy, but because we’re bringing our whole selves here today, I’m going to start with something else.

If we just type in the text at the Python REPL:

>>> Melanie is great
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Melanie' is not defined

We get an error. This, of course, throws me into some kind of existential crisis because I’m pretty sure I am defined. Let’s fix that.

If we use quotes, we don’t get an error. This is a string:

>>> "Melanie is defined."
'Melanie is defined.'
>>> "Yay! Melanie is great!"
'Yay! Melanie is great!'

Strings can use double quotes or single quotes, just make sure your quote types line up.

>>> "Yay!"
'Yay!'
>>> 'Yay!'
'Yay!'
>>> 'Boo!"
  File "<stdin>", line 1
    'Boo!"
          ^
SyntaxError: EOL while scanning string literal

That last one doesn’t work because we mixed single and double quotes together. BUT, look at this sneaky thing:

>>> "I don't need to freak out."
"I don't need to freak out."
>>> 'I don't need to freak out.'
  File "<stdin>", line 1
    'I don't need to freak out.'
           ^
SyntaxError: invalid syntax

If we have a string that needs an apostrophe in it (for example, with contractions in English), we can’t use single quotes on the outside, we have to use double quotes.

Strings can be joined together through concatenation, which is a fancy word for gluing things together. String concatenation in Python uses the + operator, the same operator that we use for addition.

>>> "This is super" + "fun!"
'This is superfun'

But that looks kind of silly. To make it look less silly, we have to put a space either in the first string or second.

>>> "This is super" + " fun!"
'This is super fun!'

Python can deal with integers also. We’ll go over more of what you can do with them later, but here’s what integers look like at the REPL:

>>> 1
1

Nothing special there. You type the number, Python spits it out.

Question: What do you think will happen if we try to concatenate a string and a number?:

>>> 3 + " is a magic number"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

We get an error telling us we can’t add integers and strings together. BUT, we can make it a string by simply surrounding it with quotes. Super sneaky. Then we can add them together.

>>> "3" + " is a magic number"
'3 is a magic number'

Time to Play! Strings 🕹

Type python3.6 (or py -3.6) in your terminal, and make sure you see >>> at the prompt.

Then, try out some strings! And not just any strings. Type fun things, silly things, and at least one super loving compliment for yourself. Like:

  • “You’re really great!”
  • “I’ve always liked that sweater!” or
  • “No one raises turtles like you do.”

Anything that gives you a wee, tiny bit of joy. Let’s practice using Python’s strings and being super nice to ourselves.