r/learnpython • u/up-with-miniskirts • 23h ago
String split and single quotation marks
I'm currently doing the Finnish Python MOOC, and at the end of chapter 4 you're not quite introduced to the split function. The problem is simple: your input a is string containing two integers, and this string must be split into two different lists, repeating for every new input string. However, the lists contain every integer within single quotation marks, which makes them unusable for mathematical operations. Am I supposed to just add another line to convert the lists into something useful? Seems really convoluted.
(And, if someone would be so kind, why does Python do this thing with the quotation marks?)
#In short, I have this:
input_string = 12 33
input_string.split()
list_a = ['12']
list_b = ['33']
#While I need this:
list_a = [12]
list_b = [33]
#So I do this:
a_no_quotes = []
for item in list_a:
a_no_quotes.append(int(item))
7
Upvotes
0
u/Brian 19h ago
This is kind of a common issue with learners, where I think there's a bit of a conceptual leap you need to make. Specifically, don't mix up the representation of a thing with the thing itself.
Ie. a string, an integer, a class and so on are objects sitting there in memory, represented some way by bytes in RAM. Think of these as the objects themselves.
However, we can't see inside computer memory - we can display text with
print, but some of those things aren't text, but we still need to display them. So what we do is create a representation of the object in text and show that. Eg. we have an integer object forty-two sitting in memory somewhere as a binary representation of the number, but when we want to print that number, we actually need to do a bit of work to convert it to a string in a familiar decimal form. So there's a bit of code that does this - it's what gets run when you callstr(my_variable)(andprintbehind the scenes will be making this call). What it does depends on the type, and so each object has special hooks that say "How should this be represented", and for integers this code looks at the memory, and builds up a string by converting the number to decimal, finding the correct character code to represent each digit, and returns it.These hooks are actually available to your own code, where you can override "How should this object be displayed". Eg:
There's also a similar special function called
__repr__, invoked by therepr(obj)function which is similar, but there more for a "programmer friendly" representation - it's what gets called when it's displaying it at the python>>>prompt. Ie. the integer42and the string"42"are very different objects, but they display the same:callingstr()on them gives you the same string. But__repr__displays them in a less ambiguous way, by adding quotes aroung the string, but leaving the integer unquoted (ie."42"vs42)And this is fundamental to what you're doing. Ie. it's not that:
It's more that your list is a list of strings - the quotation marks aren't part of them, that's just how python is displaying them, because a list displaying its contents uses the
__repr__of its elements, and those elements are strings whose__repr__displays with quotes.So a better way of thinking about this is "I have a list of strings, which I can't do maths on, because I need a list of integers instead". Hence the solution is "Convert the strings to integers", which you can do with the
int()function. Eg: