6

I'm making this Pythagoras Theorem Calculator in Python 3.3.2.

I made print over several lines so that I could make a diagram:

print("Welcome to the Pythagoras Theorem Calculator, powered by Python!")
print("Below are the values a, b and c. You will need to input these values after.")
print('''
      | .
      |   .
      |     .
side a|       . side c
      |         .
      |           .
      |_____________.
          side b
      ''')

As you can see above, three apostrophes were needed instead of the speech marks. Why is this so? Is it an escape character? (I've tried searching on Google: http://bit.ly/15a4zes)

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Turbo
  • 323
  • 3
  • 6
  • 16
  • http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals – BrenBarn Oct 04 '13 at 18:50
  • This question suggest to me that you should be able to use speech marks: http://stackoverflow.com/q/2504411/945456 – Jeff B Oct 04 '13 at 18:53
  • @JeffBridgman: You can, if you use three of them. – BrenBarn Oct 04 '13 at 18:56
  • You can use either `'''somestring'''` (I call those single quotes) or `"""somestring"""` (double quotes), but you can't mix and match. `'''somestring"""` doesn't work. – tdelaney Oct 04 '13 at 18:59
  • 2
    While "apostrophes" is not incorrect, usually programmers call them "single-quotes" (and the actual `"` quotation marks "double-quotes") – ThiefMaster Nov 18 '13 at 18:46

4 Answers4

12

The three quotes allows you to make a string on multiple lines. It avoids you to add \n everywhere or doing multiple print statements.

Threes quote strings are also used recommended to make documentation, see the PEP 257 convention (see also comments of this post)

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • I'm choosing this answer as it also provides a link and directly answers the question. – Turbo Oct 04 '13 at 18:53
  • 1
    You can make documentation with single-quoted strings too. All you need is a string literal as the first of code in your object (function, class, module). – Martijn Pieters Oct 04 '13 at 19:04
  • Oh really? I learned something today. I must have seen well-documented code everytime since I've never seen function with simple-quoted string at the beginning... Thanks :) – Maxime Lorant Oct 04 '13 at 19:05
3

They are not needed, they just make it easier to produce a multi-line string.

The alternative would be:

print('      | .')
print('      |   .')
print('      |     .')
print('side a|       . side c')
print('      |         .')
print('      |           .')
print('      |_____________.')
print('          side b')

Note that Python lets you take your pick of '..' and ".." style quotes, whatever better suits your string contents.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

Three apostrophes (or speech marks) make your string a triple-quoted string. This allows it to span multiple lines. Normal strings can not do this.

If you want the same effect with normal strings, you have to put a '\n' every time you want a line break (which is a little annoying and also makes your string hard to read).

1

The three single (''') or double (""") quotes (of course the same at the begin and at the end of string) allows us to make an output on multiple lines without adding explicite the newline character (\n) at the end of each line or without adding a multiple print functions in the source code.

Additional note:

  • Know that if you don't want to display some newline character at the output, but you want to use this transparent notation in your source code, then you can to add the character \ at the end of the line.

For example in the above case, if you don't like to display the initial newline then you can achieve it like this:

print('''\
...

And one more small note:

This works the same for Python 2 and Python 3.

simhumileco
  • 31,877
  • 16
  • 137
  • 115