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
#1
Lets say we have a string a = "apple" and we wish to add this variable in a list fruits = [], using fruits.append(a). But when we print the list, the string "apple" is stored like this in the list, ['apple']. So my question now is, why does python not store strings in double qoutes " " instead of single qoutes ' ' inside a list? or is there a workaround to store strings with double qoutes inside a list? like what's the process behind storing strings in an array or list
Reply
#2
The single or double quotes are not really part of the string, rather display helps. Python will use single quotes by default, but use double quotes if needed (if the string contains a single quote/apostrophe for example). See:
fruits = []
first = "ap'ple"
second = "grape"
third = "pear"
fruits.append(first)
print(fruits)
fruits.extend([second, third])
print(fruits)
Output:
["ap'ple"] ["ap'ple", 'grape', 'pear']
And, to access the first character of the first item in fruits[] you would access fruits[0][0] and get the letter a. No quotes, single or double.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
  Bug: Two Python unistall strings available on Windows 10 pstein 2 668 Oct-26-2025, 11:24 AM
Last Post: noisefloor
  concatenating strings with + operator CarlaRogersWI 6 1,564 Aug-10-2025, 12:23 PM
Last Post: noisefloor
  sending strings to arduino over serial/accessing DLLs HeWhoHas 0 1,415 Nov-09-2024, 06:01 PM
Last Post: HeWhoHas
  Can you explain the strings in Python ebn852_pan 3 1,761 May-19-2024, 08:36 AM
Last Post: Pedroski55
Information Do regular expressions still need raw strings? bobmon 3 5,215 May-03-2024, 09:05 AM
Last Post: rishika24
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 1,947 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 3,275 Jan-13-2024, 10:34 PM
Last Post: sgrey
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 2,203 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 1,902 Oct-04-2023, 03:08 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