Python Forum
Trying to understand strings and lists of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand strings and lists of strings
#3
It prints single quotes because that is what str.__repr__() uses to mark the start and end of the string.
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.
Reply


Messages In This Thread
RE: Trying to understand strings and lists of strings - by deanhystad - Aug-06-2023, 11:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Information Do regular expressions still need raw strings? bobmon 3 359 May-03-2024, 09:05 AM
Last Post: rishika24
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 419 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 694 Jan-13-2024, 10:34 PM
Last Post: sgrey
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 730 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 532 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 718 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  problem in using int() with a list of strings akbarza 4 801 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Taking Mathematical Expressions from Strings quest 2 753 Jul-02-2023, 01:38 PM
Last Post: Pedroski55
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,583 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Delete strings from a list to create a new only number list Dvdscot 8 1,617 May-01-2023, 09:06 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020