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
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 364 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 590 Jan-13-2024, 10:34 PM
Last Post: sgrey
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 685 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 483 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 685 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  problem in using int() with a list of strings akbarza 4 716 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Taking Mathematical Expressions from Strings quest 2 718 Jul-02-2023, 01:38 PM
Last Post: Pedroski55
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,491 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Delete strings from a list to create a new only number list Dvdscot 8 1,554 May-01-2023, 09:06 PM
Last Post: deanhystad
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 841 Apr-23-2023, 05:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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