invert the order of a string

Discussion in 'Python' started by rtilley, Feb 13, 2006.

  1. rtilley

    rtilley Guest

    s = list('some_random_string')
    print s
    s.reverse()
    print s
    s = ''.join(s)
    print s

    Surely there's a better way to do this, right?
     
    rtilley, Feb 13, 2006
    #1
    1. Advertising

  2. rtilley

    rtilley Guest

    Dave Hansen wrote:
    > How about
    >
    > s = "some random string"
    > print s
    > s = s[::-1]
    > print s


    That looks like Perl, but it works. Makes me wonder with the string
    module doesn't have a reverse or invert function?
     
    rtilley, Feb 13, 2006
    #2
    1. Advertising

  3. rtilley

    rtilley Guest

    Dave Hansen wrote:
    > It's just simple slicing. Well, maybe not so simple, or at least not
    > so common, but with a syntax similar to the range function. Consider
    > the following (string chosen to make it obvious what's going on):
    >
    > s = "0123456789"
    > s[::]
    > s[3::]
    > s[:3:]
    > s[::3]
    > s[::-2]
    > s[-2::-2]


    Well, it turns out to be the best way to invert a string, IMO. The
    reversed() feature returns a reversed object... not a reversed string.
    In short, I have to fool with it again _after_ it has been inverted. The
    slicing takes care of the job right away and gives me what I want... no
    Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)

    I'm sure the reversed feature is much more generic though for dealing
    with other types.
     
    rtilley, Feb 13, 2006
    #3
  4. rtilley

    Dave Hansen Guest

    On Mon, 13 Feb 2006 18:51:11 +0000 in comp.lang.python, rtilley
    <> wrote:

    >s = list('some_random_string')
    >print s
    >s.reverse()
    >print s
    >s = ''.join(s)
    >print s
    >
    >Surely there's a better way to do this, right?


    How about

    s = "some random string"
    print s
    s = s[::-1]
    print s

    HTH,
    -=Dave
    -=Dave

    --
    Change is inevitable, progress is not.
     
    Dave Hansen, Feb 13, 2006
    #4
  5. rtilley

    Paul Rubin Guest

    rtilley <> writes:
    > s = list('some_random_string')
    > print s
    > s.reverse()
    > print s
    > s = ''.join(s)
    > print s
    >
    > Surely there's a better way to do this, right?


    In Python 2.4, just say
    s = reversed('some_random_string')
     
    Paul Rubin, Feb 14, 2006
    #5
  6. rtilley

    Dave Hansen Guest

    On Mon, 13 Feb 2006 19:03:32 +0000 in comp.lang.python, rtilley
    <> wrote:

    >Dave Hansen wrote:
    >> How about
    >>
    >> s = "some random string"
    >> print s
    >> s = s[::-1]
    >> print s

    >
    >That looks like Perl, but it works. Makes me wonder with the string
    >module doesn't have a reverse or invert function?


    It's just simple slicing. Well, maybe not so simple, or at least not
    so common, but with a syntax similar to the range function. Consider
    the following (string chosen to make it obvious what's going on):

    s = "0123456789"
    s[::]
    s[3::]
    s[:3:]
    s[::3]
    s[::-2]
    s[-2::-2]

    Regards,
    -=Dave

    --
    Change is inevitable, progress is not.
     
    Dave Hansen, Feb 14, 2006
    #6
  7. rtilley

    Paul Rubin Guest

    rtilley <> writes:
    > Well, it turns out to be the best way to invert a string, IMO. The
    > reversed() feature returns a reversed object... not a reversed
    > string. In short, I have to fool with it again _after_ it has been
    > inverted. The slicing takes care of the job right away and gives me
    > what I want... no Computer Sciencey <reversed object at 0xb6f6152c>>
    > to deal with :)


    Oh, I see. I thought I'd tested reversed(...) but I guess I didn't.
    I'm going senile. reversed makes an iterator.

    Anyway, slicing is one solution; the array module is another.
     
    Paul Rubin, Feb 14, 2006
    #7
  8. rtilley

    Eric McGraw Guest

    > Well, it turns out to be the best way to invert a string, IMO. The
    > reversed() feature returns a reversed object... not a reversed string.
    > In short, I have to fool with it again _after_ it has been inverted. The
    > slicing takes care of the job right away and gives me what I want... no
    > Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)


    A <reversed object> can be turned back into a string:
    >>> st = '0123456789'
    >>> reversed(st)

    <reversed object at 0x00A8CC50>
    >>> ''.join( reversed(st) )

    '9876543210'

    >
    > I'm sure the reversed feature is much more generic though for dealing
    > with other types.
     
    Eric McGraw, Feb 14, 2006
    #8
  9. rtilley

    Carl Cerecke Guest

    Eric McGraw wrote:
    >>Well, it turns out to be the best way to invert a string, IMO. The
    >>reversed() feature returns a reversed object... not a reversed string.
    >>In short, I have to fool with it again _after_ it has been inverted. The
    >>slicing takes care of the job right away and gives me what I want... no
    >>Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)

    >
    >
    > A <reversed object> can be turned back into a string:
    > >>> st = '0123456789'
    > >>> reversed(st)

    > <reversed object at 0x00A8CC50>
    > >>> ''.join( reversed(st) )

    > '9876543210'


    But that's slower than new_s = s[::-1]

    Cheers,
    Carl.
     
    Carl Cerecke, Feb 14, 2006
    #9
  10. rtilley a écrit :
    > s = list('some_random_string')
    > print s
    > s.reverse()
    > print s
    > s = ''.join(s)
    > print s
    >
    > Surely there's a better way to do this, right?


    print 'some_random_string'[::-1]
     
    Bruno Desthuilliers, Feb 14, 2006
    #10
  11. Paul Rubin a écrit :
    > rtilley <> writes:
    >
    >>s = list('some_random_string')
    >>print s
    >>s.reverse()
    >>print s
    >>s = ''.join(s)
    >>print s
    >>
    >>Surely there's a better way to do this, right?

    >
    >
    > In Python 2.4, just say
    > s = reversed('some_random_string')


    Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
    [GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
    linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> reversed('some_random_string')

    <reversed object at 0x4040f30c>
    >>>


    Yes, that was my first take too - but I jumped to my Python shell before
    posting !-)
     
    Bruno Desthuilliers, Feb 14, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ally

    invert images

    ally, Jan 18, 2004, in forum: Java
    Replies:
    3
    Views:
    5,488
    Thomas Weidenfeller
    Jan 19, 2004
  2. rick
    Replies:
    42
    Views:
    2,109
    DaveM
    Oct 24, 2006
  3. Demel, Jeff
    Replies:
    7
    Views:
    286
    Carl Banks
    Oct 20, 2006
  4. Demel, Jeff
    Replies:
    3
    Views:
    291
    Duncan Booth
    Oct 20, 2006
  5. Tim Chase
    Replies:
    3
    Views:
    245
    Simon Forman
    Oct 20, 2006
Loading...

Share This Page