Returns a variable in a Python list with double quotes instead of single quotes

I am trying to return a variable from a list of strings in double quotes, and not alone.

For example, if my list

List = ["A", "B"]

if I type List[0], conclusion 'A'. Rather, I want to "A". Is there any way to do this? I need this because of an external script that runs in ArcGIS that only accepts variables in double quotes.

+4
source share
2 answers

You can use json.dumps ()

>>> import json
>>> List = ["A", "B"]
>>> print json.dumps(List)
["A", "B"]
+13
source

If you need an output formatted in a certain way, use something like str.format():

>>> print('"{0}"'.format(List[0]))
"A"

, , Python, . , .

, , Python, . , , None, repr , . .

- (, , expr):

result = expr
if result is not None:
    print(repr(result))

, print None, . , print , .

+5

Source: https://habr.com/ru/post/1607628/


All Articles