r/learnpython • u/up-with-miniskirts • 10h 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))
View all comments
Show parent comment
1
u/up-with-miniskirts 7h ago
The course material tends to be rather Spartan. Many features of the code are barely explained, with some even just showing up in model solutions without further information. In this case, the course said to use the split function, literally followed by "just google it."
My confusion between strings and ints arises from this. A number is an int when it's not in quotation marks, and a string when it is. Thus, my_string = 1 is an int, while my_string = "1" is a string. Logically, my_string = 1 2 should be an int, or a pair of ints, or some other int thing, and when you split it, it should return ints, not strings. But that's not how it is.