Hi there,

One of the ways one can reverse a string in python is like so:

``````Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2

>>> mystring = "Hello, World!"

>>> mystring[::-1]
'!dlroW ,olleH'

>>>``````

It is my understanding that here, the first argument of the slice operator defaults to the beginning of the string. The second argument defaults to the end of the string (if no argument is given). Finally, the "third argument", added relatively recently, reverses the operation when "-1" is passed.

Thus, the string is printed in reverse.

However, if the above is true, why does this happen?

``````>>> mystring[0:13]
'Hello, World!' #yup, that's fine

>>> mystring[0:13:-1]
'' #hmm, what?``````

It seems that explicitly stating the range messes up the third argument. Why? Why does it print an empty string?

Thanks.

you should use :

``````>>> mystring[13:0:-1]
'!dlroW ,olle'``````

But the first letter isn't taken as the second parameter of the slice is to be excluded... To have the first letter (H), you'll need to do

``````>>> mystring[13::-1]
'!dlroW ,olleH'``````

All 3 Replies

you should use :

``````>>> mystring[13:0:-1]
'!dlroW ,olle'``````

But the first letter isn't taken as the second parameter of the slice is to be excluded... To have the first letter (H), you'll need to do

``````>>> mystring[13::-1]
'!dlroW ,olleH'``````

jice is right. You're telling it to start at 0 and end at 13, but your step is -1, so it won't work. I think you're a little confused; the -1 does not 'reverse' the operation. It sets the amount it steps by each time...

So if I set it to 2, it would increment over the string by 2 indices each time, like this:

``````>>> a = 'Hello World!'
>>> a[0:13:2]
'HloWrd'``````

So a negative step simply means it'll iterate backwards by that number. You still need to adjust your range's start and end indices accordingly though.

Hi there,

One of the ways one can reverse a string in python is like so:

``````Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2

>>> mystring = "Hello, World!"

>>> mystring[::-1]
'!dlroW ,olleH'

>>>``````

It is my understanding that here, the first argument of the slice operator defaults to the beginning of the string. The second argument defaults to the end of the string (if no argument is given). Finally, the "third argument", added relatively recently, reverses the operation when "-1" is passed.

Thus, the string is printed in reverse.

However, if the above is true, why does this happen?

``````>>> mystring[0:13]
'Hello, World!' #yup, that's fine

>>> mystring[0:13:-1]
'' #hmm, what?``````

It seems that explicitly stating the range messes up the third argument. Why? Why does it print an empty string?

Thanks.

``mystring[0:13:-1]``

-will NOT give a reversed string but an empty string, since it would try to iterate from index position 0 to 13, but by a stepping of -1, which is logically incorrect.

So to reverse a string:

``````mystring="katamole"
print(mystring[13::-1])``````

Hope I helped! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.