[Solved] Python: ASCII letters slicing and concatenation [closed]


I think you are misunderstanding the “:” notation for the lists.

  • upper[:3] gives the first 3 characters from upper whereas
  • upper[3:] gives you the whole list but the 3 first characters.

In the end you end up with :

upperNew = upper[:3] + upper[3:]  
         = 'ABC'     + 'DEFGHIJKLMNOPQRSTUVWXYZ'

When you sum them into upperNew, you get the alphabet.

It happens the same thing the second time in a and b, but you concatenate them in the reversed order, so you get 'DEFGHIJKLMNOPQRSTUVWXYZ' + 'ABC'+ which is probably why you seem confused.

If you want upperNew to give the same result, you have to do it this way :

upperNew = upper[3:] + upper[:3] # Note I switched the right part  
print upperNew

Then 'DEFGHIJKLMNOPQRSTUVWXYZABC' is printed as expected.

solved Python: ASCII letters slicing and concatenation [closed]