invert the order of a string

R

rtilley

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?
 
R

rtilley

Dave said:
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?
 
R

rtilley

Dave said:
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.
 
D

Dave Hansen

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
 
P

Paul Rubin

rtilley said:
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')
 
D

Dave Hansen

Dave said:
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
 
P

Paul Rubin

rtilley said:
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.
 
E

Eric McGraw

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 :)
'9876543210'
 
B

Bruno Desthuilliers

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]
 
B

Bruno Desthuilliers

Paul Rubin a écrit :
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.
Yes, that was my first take too - but I jumped to my Python shell before
posting !-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top