6

I would like to ask, how to use print() in python3 to print the double quote (compulsory).

The code:

>>> List0 = ["1", "text", 25]
>>> print(List0)
['1', 'text', 25]
>>> str0 = str(List0)
>>> print(str0)
['1', 'text', 25]

str0 looks like a list but it is a string. Using print(), the double quotes are not shown. How to use the print(), but let the quotes shown? It should be like: "['1', 'text', 25]" ?

Thanks for the help!

0

5 Answers 5

15

It looks like you want to print what the string would look like if you were to assign it as a literal. For that, use repr, like this:

print(repr(str0))

will print

"['1', 'text', 25]"
Sign up to request clarification or add additional context in comments.

2 Comments

Since they're in the console, they can just type str0
yes, but the question was how to use print() to get this result.
2

str.format does it all very simply: list to string conversion, formatting:

>>> List0 = ["1", "text", 25]
>>> print('"{}"'.format(List0))
"['1', 'text', 25]"

Comments

2

You can use the json module like so:

import json

list0 = ["1", "text", 25]

print(json.dumps(list0)) #=> ["1", "text", 25]

Edit

To have double quotes on the outside use Python 3.6+ string interpolation:

list0 = ["1", "text", 25]
print(f'"{list0}"')
"['1', 'text', 25]"

2 Comments

that doesn't add the quotes, and changes simple quotes by double quotes
you need python 3, but python 3.6 and higher, though: stackoverflow.com/questions/35745050/…
0

You can escape from a string by using the character \:

print("\"['1', 'text', 25]\"")  # "['1', 'text', 25]"

Alternatively you can use triple quotes:

print('''"['1', 'text', 25]"''')  # "['1', 'text', 25]"

Comments

0

Print will not show quotes by default in a string. You will have to add them by concatenation.

Print("\""+str0+"\"")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.