r/learnpython • • 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))
5 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/up-with-miniskirts 9h ago

I think I got thrown off a bit by split() returning strings in single quotes, while the course material always uses double quotes (though it's mentioned that they're interchangeable).

1

u/MezzoScettico 7h ago

Neither of those is part of the string. It's just the way the string is displayed to YOU, so you understand it's a string, or the way you tell Python that an object is a string. As far as I know, both single or double quotation marks are legitimate for this purpose and completely interchangeable.

Even the Style Guide doesn't make a recommendation either way. It's up to you (or the leads if you're working on a team).

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.

1

u/MezzoScettico 7h ago edited 7h ago

my_string = 1 2 should be interpreted as invalid, and indeed when I type it into my Python interpreter I get a syntax error. Does your Python actually accept your code?

my_string = (1, 2) would be interpreted as a tuple of ints.

my_string = [1, 2] would be interpreted as a list of ints.

Neither type of object has a split method, so the "if you split it" question is not meaningful.

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):

results = input("Exam points and exercises completed: ") #input consists of two numbers
exam_list = []
exercise_list = []
temp_list = results.split()
exam_list.append(temp_list[0])
exercise_list.append(temp_list[1])

exam_list_int = []
for item in exam_list:
    exam_list_int.append(int(item))

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.

1

u/MezzoScettico 3h ago

I'm confused about the "two lists" thing. If you have only two numbers total, where are the lists coming from?

Could you show an example of typical input and what the output would look like if you did this with pencil and paper?

1

u/up-with-miniskirts 2h ago

The results = input("") line is supposed to be run until halted by the user. Things were difficult enough as they were, so I left that out.

I hope I can leave the whole int vs. string problem behind me now, though. It's far from the first time I've banged my head against it.