Strings in Python

Strings are values that consist of text. For example:


Either single quotes or double quotes are fine, as long as you're consistent. When does it matter when you're using single quotes or double quotes? When you want to have quotes as part of the text. First, let's try the really naive approach to doing this

In [2]:
"Artsies are "smart""
  File "<ipython-input-2-88cf1b2d1e33>", line 1
    "Artsies are "smart""
SyntaxError: invalid syntax

WHy is there a problem there? because Python understands that "Artsies are" is a string, and thinks that smart is some kind of variable. (If if we had a variable named smart, the line still wouldn't make sense, since you need some kind of connector between the string and the variable smart.

One way to solve this is to mix the style of quotes. Both of the following are fine:

"Artsies are 'smart'"
'Artsies are "smart"'

What if we insist on just using double quotes? Python allows us to do this by using the backslash

In [1]:
"Artsies are \"smart\""
'Artsies are "smart"'

\" is interpreted by Python as a double quote that is not the end of a string.

What if we want a string that contains a slash follows by a quote?

In [2]:

Multiline strings

The following will not work:

In [3]:
  File "<ipython-input-3-d49ff857fc6c>", line 1
SyntaxError: EOL while scanning string literal

However, it is possible to define multiline strings by enclosing text in a pair of triple single quotes:

In [6]:
a = '''abc

This is how a gets printed. We can also display a the way we would enter it:

In [7]:

Let's enter a string that way:

In [8]:
b = 'abc\ndef\ngheijklmn'

"\n" represents a newline: an instruction to print to switch to a new line. Again, if we want a string to literally contain a backslash followed by an n, we would go

In [10]:

"\\" is interpreted as a single backslash in the string -- it's just a way to enter a single slash without python interpreting the backaslash as the first part of "\n".

One way that multiline strings are often used is to "comment-out" code in your programs. For example, if I have the following in my program, the program will still run fine:

In [11]:

52 + 3
>>> 'Artsies are "smart"'
'Artsies are "smart"'

>>> "Artsies are 'smart'"
"Artsies are 'smart'"

>>> "Artsies are \"smart\""
'Artsies are "smart"'

print("nothing interesting happenned above")
nothing interesting happenned above

This worked because Python allows you to enter expressions into the program. The expressions (like, 42, and 52 + 3, and the multiline string) have no effect on the output, since you are not printing them, but they are also not interfering with the program's being able to run.

Sometimes, using multi-line strings as comments is the correct thing to do. However, most of the time, if you want to comment out a bunch of code, use the editor (in Pyzo, go Edit->Comment after highlighting the chunk of code you want to comment out). It should look something like this:

# >>> 'Artsies are "smart"'
# 'Artsies are "smart"'
# >>> "Artsies are 'smart'"
# "Artsies are 'smart'"
# >>> "Artsies are \"smart\""
# 'Artsies are "smart"'