Hello im not sure why i get cba as output when i write separators = char + separators compared to abc when i write as separators = + separators + char

line = ‘abc123’
separators = ’ ’

for char in line:
if not char.isnumeric():
separators = char + separators

Think about what separators = char + separators means. You can try to evaluate this code on paper and you might see why it comes out cba.

Hello, thanks for replying!

I am still not too sure about it even after writing down, do you mind explaining to me or giving me more information about this!

Thank you very much in advance!

Well, adding a print statement can help you see how this unfolds:

>>> message = "hello world"
>>> string = " :atip"
>>> for i in string:
...     print(i + message)
...     message = i + message
 hello world
: hello world
a: hello world
ta: hello world
ita: hello world
pita: hello world
>>> print(message)
pita: hello world

I think i have a clearer idea now, thanks!

To clarify further: the way i state the variable is important as the order (the variable printed normally or reverse) will be determined by it?

Like print ( i + message) or print (message + i) - the order in how i write which plus which

1 Like

Yes. An interesting thing to notice is that using the + operator is concatenating rather than adding. They might feel similar, but they have the key distinction that addition is symmetric (4+5==5+4) while concatenation is not (“he” + “llo” != “llo” + “he”).

1 Like