ChuNan
ChuNan

Reputation: 1141

How to display the list with "" in python

In Python it is allowed to use both '' and "" in the list.

And when I generate a list like:

map(str,list1)

the default display uses ' ':

mylist = ['1','2','3']

Now I want to display like :

mylist = ["1","2","3"]

How can I make it?

Upvotes: 0

Views: 102

Answers (2)

Sjshovan
Sjshovan

Reputation: 320

You can also use the built-in string.format(). Try this:

>>> Mylist = [1, 2, 3]
>>> Mylist = ['"{}"'.format(x) for x in Mylist]

This will replace the {} with each element in Mylist and it to the new list , in its new format. You will get this result :

>>> Mylist
['"1"', '"2"', '"3"']
>>> print Mylist[0]
"1"

Hope this is what you are looking for, best of luck!

Upvotes: 1

Pavel
Pavel

Reputation: 7552

you can just replace the output after converting the list to a string:

>>> x = ['a','b']
>>> x
['a', 'b']
>>> y = str(x)
>>> y
"['a', 'b']"
>>> y.replace("'", '"')
'["a", "b"]'

note however, this is a string now, not a list.

Upvotes: 2

Related Questions