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 6h ago
Yeah, you're right. I've messed up user input and hard-coded integers here.
The actual code looks like this (I think it works as is, but it's not important):
So, you start with two numbers, which are integers, but in the mathematical sense only. You actually have a string of two numbers, which needs to be split into into its substrings, into two different lists. Both lists of substrings then need to be converted into lists of integers. Split has not been explained, and neither has been how to convert a list of strings into a list of integers.
And then I try to get my half-baked code to work, and I only end up understanding even less.