[Courses] [python] Lesson 1: Hello world

Akkana Peck akkana at shallowsky.com
Wed Jun 22 19:29:57 UTC 2011


Holly Bostick writes:
> Hey, sorry to be late to class, but at least I've got my homework :) .

Being late is no problem. And anyway, you're not late since lesson 2
won't be posted for two more days. :-)

> In wondering about why I overthought it after reading the rest of the
> replies, I realized that I had not understood that *the entire
> line*comprises the 'print' statement, not just what was in the quotes.
> So now I
> want to know-- how do you know when the command has 'ended' ? Is it just by
> putting a hard return to the next line, as in the extended example that
> includes user input? If so, what if I wanted to print two lines that had a
> hard return between them? For example, if I wanted the answer to "What is
> your name" to be printed on the next line (which would look better)? I
> imagine there's an escape character to signify that....?

Oh, all you students asking questions that get way ahead of the lessons. :-)

But they're great questions! Python does indeed use the newline to
indicate the end of a statement (unlike many other modern languages
that use punctuation like ; and { } for such details).

So what about printing strings with newlines? There are two ways.

First, you can use \n to indicate a newline. So if you want to print
out three words each on their own line, you could say

print "one\ntwo\nthree"

Nearly every modern programming language understands \n, so it's
worth learning it even if you eventually switch to a language other
than Python.

But Python has an even nicer way: triple quotes. You can say:

print """one
two
three"""

-- start with three double quotes, or three single quotes, and your
string can include as many newlines as you need.

Triple-quoted strings are great for when you're doing things like
generating web pages -- you can take your raw HTML and paste it into
a single string and print it out with one print statement. They're
also great for documentation and for showing instructions to the user.

If anyone's feeling overwhelmed by all this, don't worry about
things like triple quotes and \n! These are more advanced topics
and you can always pick up stuff like that later on after you're
comfortable with the basics. They won't be on the quiz. :-)

> My other question is about that space. If the space in the output "Hello,
> Holly" is added by the comma, then the space between , and name is a "null
> character"?  (Yes, indeed it is; I changed ", name" to ",name" and I still
> got "Hello, Holly".) So does that mean that the space between the Python
> function signifier and the remaining content is for human-readability
> purposes, but otherwise not strictly required?

That's right -- it's purely for human readability, and many
programmers don't put spaces after commas.

Farhan asks:
> 	Is there a benefit to using single quotes instead of double in Python?
> 	Are single quotes more literal?

Leslie writes:
> I am a beginner myself, but I read somewhere recently that there is
> actually a disadvantage to using single quotes instead of double quotes.
> If you start a string with a a single quote, and the string contains an
> apostrophe (example: 'Here's your milk.'), Python will see the
> apostrophe as the end of the string ('Here' would be where it would stop
> reading the string).

I agree with that, though it's also not that uncommon to need
strings that include double quotes inside them, so you might have
to switch back and forth, depending on what you're quoting.

If you have a string that includes both single and double, we've
already talked about using \ to escape them:

print "This is Akkana's \"Hello world\" program"

but that's another place where triple-quotes are great:

print """This is Akkana's "Hello world" program"""

Much easier to read! Triple single quotes ''' would have worked just as well.

	...Akkana


More information about the Courses mailing list