Normally, Python will print a string without quotes. That's standard in almost all programming languages.
However, Python does let you print a string with the quotes, you just need to tell it to print the representation of the string. One way to do that is with the !r
formatting directive.
The items in a
are integers, if you want them printed as strings you need to convert them to strings.
a = [0, 1, 2, 3]
b = ['0','1','2','3']
a1 = a[0]
b1 = b[0]
print('{!r}'.format(str(a1)))
print('{!r}'.format(b1))
output
'0'
'0'
You can read about the representation of a string in the official tutorial.
There are other ways to see the representation of a string. For example, you can call the built-in repr
function:
print(repr(b1))
Bear in mind that if a string already contains single-quote chars then its representation will be enclosed in double-quotes:
s = "A 'test' string"
print(repr(s))
output
"A 'test' string"
And if the string contains a mixture of quotes then the representation will revert to using single quotes, and using backslash escapes to denote single quotes:
s = "A 'test' \"string\""
print(repr(s))
output
'A \'test\' "string"'
So if for some reason you don't want that behaviour, then you will need to print the quote marks explicitly:
s = "A 'test' string"
print("'{}'".format(s))
output
'A 'test' string'