93

When I want to do a print command in Python and I need to use quotation marks, I don't know how to do it without closing the string.

For instance:

print " "a word that needs quotation marks" "

But when I try to do what I did above, I end up closing the string and I can't put the word I need between quotation marks.

How can I do that?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Thi G.
  • 1,578
  • 5
  • 16
  • 27

12 Answers12

223

You could do this in one of three ways:

  1. Use single and double quotes together:

    print('"A word that needs quotation marks"')
    "A word that needs quotation marks"
    
  2. Escape the double quotes within the string:

    print("\"A word that needs quotation marks\"")
    "A word that needs quotation marks" 
    
  3. Use triple-quoted strings:

    print(""" "A word that needs quotation marks" """)
    "A word that needs quotation marks" 
    
Neuron
  • 5,141
  • 5
  • 38
  • 59
Jamie Forrest
  • 10,895
  • 6
  • 51
  • 68
  • 5
    Escaping the double-quotes works, but the extra spaces you've put in are now part of the string, easily fixed by getting rid of the spaces. Similarly with triple-quoted strings - the extra spaces become part of the string, and I'm not sure how to fix this as putting the inner " next to the triple-" causes a SyntaxError – James Polley Jan 29 '12 at 02:21
  • 3
    You can avoid the SyntaxError by using single triple quotes: '''"a word"''', but now things are getting silly.. – DSM Jan 29 '12 at 02:41
  • 1
    If you have several words like this which you want to concatenate, I would use [this answer](https://stackoverflow.com/a/51020790/5608610). There is also a Python 3.6 specific answer given which is very readable. – glarys Jul 10 '18 at 15:56
  • 4) `print('\'A word that needs quotation marks\'')` – Barbaros Özhan Sep 19 '19 at 13:18
20

You need to escape it.

>>> print("The boy said \"Hello!\" to the girl")
The boy said "Hello!" to the girl
>>> print('Her name\'s Jenny.')
Her name's Jenny.

See the python page for string literals.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
7

Python accepts both " and ' as quote marks, so you could do this as:

>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"

Alternatively, just escape the inner "s

>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
James Polley
  • 7,977
  • 2
  • 29
  • 33
5

Use the literal escape character \

print("Here is, \"a quote\"")

The character basically means ignore the semantic context of my next charcter, and deal with it in its literal sense.

yurisich
  • 6,991
  • 7
  • 42
  • 63
2

When you have several words like this which you want to concatenate in a string, I recommend using format or f-strings which increase readability dramatically (in my opinion).

To give an example:

s = "a word that needs quotation marks"
s2 = "another word"

Now you can do

print('"{}" and "{}"'.format(s, s2))

which will print

"a word that needs quotation marks" and "another word"

As of Python 3.6 you can use:

print(f'"{s}" and "{s2}"')

yielding the same output.

Cleb
  • 25,102
  • 20
  • 116
  • 151
1

One case which is prevalent in duplicates is the requirement to use quotes for external processes. A workaround for that is to not use a shell, which removes the requirement for one level of quoting.

os.system("""awk '/foo/ { print "bar" }' %""" % filename)

can usefully be replaced with

subprocess.call(['awk', '/foo/ { print "bar" }', filename])

(which also fixes the bug that shell metacharacters in filename would need to be escaped from the shell, which the original code failed to do; but without a shell, no need for that).

Of course, in the vast majority of cases, you don't want or need an external process at all.

with open(filename) as fh:
    for line in fh:
        if 'foo' in line:
            print("bar")
tripleee
  • 175,061
  • 34
  • 275
  • 318
0

in Python 3.2.2 on Windows,

print(""""A word that needs quotation marks" """) 

is ok. I think it is the enhancement of Python interpretor.

0

You could also try string addition: print " "+'"'+'a word that needs quotation marks'+'"'

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
0

I'm surprised nobody has mentioned explicit conversion flag yet

>>> print('{!r}'.format('a word that needs quotation marks'))
'a word that needs quotation marks'

The flag !r is a shorthand of the repr() built-in function1. It is used to print the object representation object.__repr__() instead of object.__str__().

There is an interesting side-effect though:

>>> print("{!r} \t {!r} \t {!r} \t {!r}".format("Buzz'", 'Buzz"', "Buzz", 'Buzz'))
"Buzz'"      'Buzz"'     'Buzz'      'Buzz'

Notice how different composition of quotation marks are handled differenty so that it fits a valid string representation of a Python object 2.


1 Correct me if anybody knows otherwise.

2 The question's original example " "word" " is not a valid representation in Python

Leonardus Chen
  • 1,103
  • 6
  • 20
0

This worked for me in IDLE Python 3.8.2

print('''"A word with quotation marks"''')

Triple single quotes seem to allow you to include your double quotes as part of the string.

dalhax
  • 13
  • 2
0

Enclose in single quotes like

print '"a word that needs quotation marks"'

Or Enclose in Double quotes

print "'a word that needs quotation marks'"

Or Use backslash \ to Escape

print " \"a word that needs quotation marks\" "
0

In case you don't want to use escape characters and actually want to print quote without saying " or \" and so on, you can tell python to print the ASCII value of the " character. The quote character in ASCII is 34, (single quote is 39)

In python 3

print(f'{chr(34)}')

Output: "

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 12:45