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

Or on Windows:

$ py -3

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 (or py -3) 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.