1

Possible Duplicate:
reverse a string in Python

I tried searching the python documentation for a certain slicing exapmle lets say I have this string

a = "ABCD"

when I write:

a[::-1]

I get the reversed string

"DCBA"

I can't understand how exectaly it works. None of the examples I saw were with two colons. what does it mean? how does it work?

thank you!

Community
  • 1
  • 1
user1073865
  • 2,383
  • 3
  • 15
  • 7
  • 2
    um... why would you downvote this? Vote to close as duplicate - yes, but why be rude to a new user? – MK. Jan 14 '12 at 22:05

1 Answers1

4

The full syntax of a slicing is

a[start:stop:step]

The step value denotes by how much to increase the index when going from one element of the slice to the next one. A value of -1 consequently means "go backwards". If start and stop are omitted as in your example, the default is to use the whole string (or more generally, the whole sequence, as this also works for lists and tuples).

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841