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!
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]"
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]"
str.format
does it all very simply: list to string conversion, formatting:
>>> List0 = ["1", "text", 25]
>>> print('"{}"'.format(List0))
"['1', 'text', 25]"
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]"
Print will not show quotes by default in a string. You will have to add them by concatenation.
Print("\""+str0+"\"")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.