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:
- Reads each character you type
- Executes the code you type
- Prints out the resulting response, if any
- 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.