I have a list I want to pretty print that contains empty lists as well as lists with string members. the problem is that lists that contains strings are printed with double quotes:
>>>str(['a'])
"['a']"
But an empty list is printed with single quotes:
>>> str([])
'[]'
Is there a way to always force printing string with double quotes ?
It depends on the representation of the object being printed; if the string to print contains the \\"
character, then a single quote will be used; if the string contains the \\'
character, then a double quote will be used.
Use custom string formatting:
print '"{}"'.format(str([]))
prints
"[]"
This won't affect strings nested in containers, though:
print '"{}"'.format(str(["a"]))
prints
"['a']"
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.