Home arrow Python arrow Page 3 - Python 3.1: Strings and Quotes

Understanding Different Quotes - Python

In this second part of a three-part series that introduces you to Python, you'll learn about the importance of strings, how they work, and why Python uses three different kinds of quote marks. It is excerpted from the book Beginning Python: Using Python 2.6 and Python 3.1,, written by James Payne, Developer Shed Editor-in-Chief (Wrox, 2010; ISBN: 0470414634).

  1. Python 3.1: Strings and Quotes
  2. Why Three Types of Quotes?
  3. Understanding Different Quotes
  4. Putting Two Strings Together
By: James Payne
Rating: starstarstarstarstar / 2
May 11, 2010

print this article



Now that you know how to use the print() function, you can begin to work with the different types of quotes discussed earlier in this chapter. Try the examples from earlier:

> > > print(‘This is a string using a single quote!’)

This is a string using a single quote!

> > > print(“This is a string using a double quote!”)

This is a string using a double quote!

> > > print(“””This string has three quotes!

Look at what it can do!”””)

This string has three quotes

Look at what it can do!

In this example, you see that the single quote ( ' ) and double quote ( " ) are interchangeable in those instances. However, when you want to work with a contraction, such as don’t, or if you want to quote someone quoting something, observe what happens:

> > > print(“I said, “Don’t do it”)

When you press Enter to execute the function, you will get the error message: SyntaxError: invalid syntax ( < pyshell#10 > , line 1). I know what you are thinking — “ What happened? I thought double and single quotes are interchangeable. ” Well, they are for the most part. However, when you try to mix them, it can often end up in a syntax error, meaning that your code has been entered incorrectly, and Python doesn’t know what the heck you are trying to say.

What really happens here is that Python sees your first double quote and interprets that as the beginning of your string. When it encounters the double quote before the word Don’t , it sees it as the end of the string. Therefore, the letters on make no sense to Python, because they are not part of the string. The string doesn’t begin again until you get to the single quote before the t .

There is a simple solution to this, known as an escape. Retry the preceding code, adding an escape

character to this string:

> > > print(“I said, \”Don’t do it”)

I said, “Don’t do it

This time, your code worked. When Python saw the backslash (\), or escape character, it knew to treat the double quote as a character, and not as a data type indicator. As you may have noticed, however, there is still one last problem with this line of code. See the missing double quote at the end of your results? To get Python to print the double quote at the end of the sentence, you simply add another escape character and a second double quote, like so:

> > > print(“I said, \”Don’t do it\””)

I said, “Don’t do it”

Finally, let’s take a moment to discuss the triple quote. You briefly saw its usage earlier. In that example, you saw that the triple quote allows you to write some text on multiple lines, without being processed until you close it with another triple quote. This technique is useful if you have a large amount of data that you do not wish to print on one line, or if you want to create line breaks within your code. Here, in the next example, you write a poem using this method:

> > > print(“””Roses are red

Violets are blue

I just printed multiples lines

And you did too!”””)

Roses are red

Violets are blue

I just printed multiple lines

And you did too!

There is another way to print text on multiple lines using the newline ( \n ) escape character, which is the most common of all the escape characters. I’ll show it to you here briefly, and come back to discuss it in more depth in a later chapter. Try this code out:

> > > print(“Roses are red \n Violets are blue \n

I just printed multiple

lines \n And you did too!”)

Roses are red

Violets are blue

I just printed multiple lines

And you did too!

As you can see, the results are the same. Which you use is up to you, but the newline escape is probably more efficient and easier to read.

>>> More Python Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: