Rabu, 08 Desember 2021

Using quotation marks inside quotation marks

You could do this in one of three ways:Use single and double quotes together:print('"A word that needs quotation marks"')"A word that needs quotation marks"Escape the double quotes within the string:print("\"A word that needs quotation marks\"")"A word that needs quotation marks"Use triple-quoted strings:print(""" "A word that needs quotation marks" """)"A word that needs quotation marks"

answered Jan 29 '12 at 2:19

10.4k66 gold badges4949 silver badges6767 bronze badges


You need to escape it. (Using Python 3 print function):>>> 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.

answered Jan 29 '12 at 2:17

Jonathon ReinhartJonathon Reinhart

121k2727 gold badges234234 silver badges308308 bronze badges

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"

answered Jan 29 '12 at 2:16

7,52922 gold badges2626 silver badges3232 bronze badges

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.

answered Jan 29 '12 at 2:19

6,73955 gold badges4040 silver badges6161 bronze badges

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 doprint('"{}" 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.

answered Jun 25 '18 at 10:01

21.6k1818 gold badges9696 silver badges136136 bronze badges

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 withsubprocess.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")

answered Nov 24 '17 at 8:46

149k2626 gold badges224224 silver badges281281 bronze badges

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.

answered Apr 11 '12 at 8:04

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

answered Aug 30 '17 at 0:58


6,36344 gold badges2222 silver badges4040 bronze badges

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 representationobject.__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

answered Dec 7 '20 at 9:24

This worked for me in IDLE Python 3.8.2print('''"A word with quotation marks"''')

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

answered Dec 16 '20 at 17:42

1322 bronze badges

Enclose in single quotes likeprint '"a word that needs quotation marks"'

Or Enclose in Double quotesprint "'a word that needs quotation marks'"

Or Use backslash \ to Escapeprint " \"a word that needs quotation marks\" "

answered Nov 25 at 7:35

Not the answer you're looking for? Browse other questions tagged python string python-2.7or ask your own question.

Tidak ada komentar:

Posting Komentar

Cart2Quote: Quotation Extension for Magento

Create, send and manage quotes in Magento quickly and easily Receive quote requests through your front-end or start Quotations from your Ma...