Lesson 1-2 covered which line correctly prints the word hello.
Text must sit inside quotes so Python treats it as a string, and print needs parentheses around what you hand it.
print(hello) without quotes makes Python look for something named hello, which does not exist yet, and that causes an error.
Strings, line by line
Most of the data real programs handle is text: usernames, chat messages, search queries, email subjects, file names. An engineer's average day involves far more reading, checking, and assembling of text than fancy math, which is why strings get their own lesson before anything else.
A string is any piece of text your program works with: a name, a sentence, an emoji, even "12345" if you mean it as text rather than a number. You met strings in lesson 1-2, and here are some useful details.
Each print produces one line of output, and the interpreter runs your lines top to bottom. So three prints make three lines, in order:
print("first") print("second") print("third")
Python accepts either double quotes "like this" or single quotes 'like this'. They mean the same thing. Pick one style and stay consistent, and this course uses double quotes.
Strings also work with +. Between two strings, + produces a new string: the first immediately followed by the second. Joining strings this way is called concatenation:
print("door" + "bell")
That prints doorbell. Note that + adds no space. Concatenation joins the strings exactly as they are, so if you want a space you must put one inside a string yourself.
Three lines, in order
The output lines appear in the same top-to-bottom order as the code.
print("Good morning.") print("Good afternoon.") print("Good evening.")
Output
Good morning. Good afternoon. Good evening.
Reordering the lines would reorder the output, because Python runs them top to bottom and nothing else decides the sequence.
That sounds obvious, and it is the foundation of reading any program. When you want to know what happens first, you look higher up the file. Later units introduce the only three things that change this order: choices, repetition, and functions.
Concatenating two words
print("rain" + "bow") outputs rainbow.
The + operator concatenates the two strings with nothing at all in between. Python adds no separator, no space, and no punctuation, because it has no way to know you wanted any.
If you wanted a space you would put one inside one of the strings yourself, like "rain " + "bow". The space is a character in the text, exactly like the letters around it.
That is worth internalizing early, because it is the source of the most common cosmetic bug in beginner code: output that reads Hello,Ada instead of Hello, Ada.
Three sentences, three prints
Three sentences means three separate print lines.
print("Code is just text.") print("Computers follow it exactly.") print("You are the author.")
Output
Code is just text. Computers follow it exactly. You are the author.
Reading the pieces
- Each sentence goes in its own quotes inside its own
print, and the punctuation lives inside the quotes because the period is part of the text. - The three lines run top to bottom, so the output order is decided entirely by the source order.
- One
printper line of output is the rule for now. There are ways to print several lines from one call, but reaching for them before you need them makes code harder to read rather than shorter.
Two more string tools
Repetition. Between a string and a whole number, * produces the string repeated that many times. Python reuses the multiplication symbol because repeating is repeated addition, so "ha" * 3 means "ha" + "ha" + "ha". Engineers use this for things like separator lines in reports and padding in tables.
Quotes inside quotes. A double-quoted string ends at the next double quote, so "He said "hi"" confuses Python, which thinks the string ended before hi. There are two clean fixes. Wrap the string in single quotes when the text contains double quotes, or put a backslash before the inner quote as \", which tells Python that this quote is part of the text rather than the end of the string.
print('It said "hello" to me.') print("It said \"hello\" to me.")
Both lines print the same sentence, quotes included. The backslash form is called an escape, and it is how you include any character that would otherwise have a special meaning.
Repetition and an escaped quote
The first line repeats a string, and the second keeps a double quote inside a double-quoted string.
print("ha" * 3) print("It said \"hello\" to me.")
Output
hahaha
It said "hello" to me."ha" * 3 builds one string, hahaha, before print ever sees it. The repetition happens first, and print receives a single finished six-character string with no idea it was assembled.
That order matters more than it looks. Python evaluates what is inside the parentheses down to one value, then hands that value over, which is the same inside-out reading you used for input() in lesson 1-3.
A divider line without typing 20 dashes
String repetition builds the divider from a single character.
print("-" * 20)
Output
--------------------
Reading the pieces
- The string being repeated is one dash,
"-", and the number says how many copies to make. - Writing it this way makes the width obvious and changeable. Anyone reading
"-" * 20knows immediately that it is 20 characters wide, which is not true of 20 dashes typed by hand. - It is also impossible to get wrong. Typed by hand, 19 or 21 dashes looks identical on screen and quietly misaligns a report.