r/learnpython • u/up-with-miniskirts • 2d 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))
3
u/00HoppingGrass00 2d ago
That's not quite how split works. Split returns one list containing all of the substrings, so splitting "12 33" gives you ["12", "33"] rather than two lists.
Also it makes sense that splitting a string will give you strings, no? Python won't magically know that the substrings can be converted to integers.
6
u/Insomnia_Calls 2d ago edited 2d ago
Your solution is good. Just a few notes:
Edit: changed wording in 3.
3
u/desrtfx but other languages pro 2d ago
Please, tell us the exact exercise.
1
u/codeguru42 1d ago
It looks like you are explaining your solution to a problem without explaining the problem. In addition, your solution shows you are missing some important details. We can help more easily explain and fill in the gaps if you also give the problem statement that you are writing this program to solve.
1
u/POGtastic itertools evangelist 2d ago
Strings of text could be anything.
We also have an addition operation for strings (concatenation). So yes, you need to call
intto parse those strings into integers, which throws an exception if the conversion fails.You will generally find that the more implicit a language is with things like type conversions, the harder it is to keep track of what is happening. Javascript programmers have an absolutely miserable time with this.
Specifically for mapping a function onto a list of elements, Python provides list comprehensions and the
mapbuiltin.0
u/Brian 2d 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:0
u/VadumSemantics 2d ago
I will encourage you to try some simpler expressions.
Adding
print()to your code is an easy way to see what it is doing. Once you see what the types & values are you can comment out the print.I'll going to introduce the
type()function, which may seem like a bit of a cheat.But knowing the type of some value can be super useful.
example code
output
edit: fixed copy/paste fail on output.
-1
u/danielroseman 2d ago
You can use a list comprehension.
Note, the problem is not the "single quotation marks", it is that splitting a string still gives you a string, but you want integers.
-1
u/woooee 2d ago
There is no reason to do this as there is no reason to create a list that contains one string. Just use the original string.
can just be
Note that if you want to split a number, use divmod and divide by one