Aug-06-2023, 11:42 AM
(This post was last modified: Aug-06-2023, 11:25 PM by deanhystad.)
It prints single quotes because that is what str.__repr__() uses to mark the start and end of the string.
The quote characters at the start and end of a string literal are not part of the string. They are there to tell Python where the string starts and ends. When Python converts the string literal to a str object, the new str object doesn't have any quotes.
def print_string(string): print(string, repr(string)) print_string("apple") print_string('apple') print_string("""apple""")
Output:apple 'apple'
apple 'apple'
apple 'apple'
When you print a string, Python calls str.__str__() to get a pretty representation of the str object. The pretty representation has no surrounding quotes. When you print a list of strings, Python calls str.__repr__() to get a more informative representation of the string. The informative representation is enclosed in single quotes to tell the user 'This is a str object'.The quote characters at the start and end of a string literal are not part of the string. They are there to tell Python where the string starts and ends. When Python converts the string literal to a str object, the new str object doesn't have any quotes.