Custom Search

Wednesday, February 16, 2011

python django Reverse list or string different methods

python django Reverse list or string different methods

>>> a = range(11)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a[::-1] <-------
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> a[len(a)::-1] <-------
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> a[len(a):0:-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

>>> a[len(a):1:-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2]

------------------

>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a.reverse()
>>> a
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

------------------

>>> name = "python django"
>>> name[::1]
'python django'

>>> name[::2]
'pto jno'

>>> name[::-1] <------
'ognajd nohtyp'

>>> name[::-1]

--------------------------------

No comments:

Post a Comment