473,480 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

invert the order of a string

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?
Feb 14 '06 #1
10 3697
On Mon, 13 Feb 2006 18:51:11 +0000 in comp.lang.python, rtilley
<rt*****@vt.edu> 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.
Feb 14 '06 #2
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?

Feb 14 '06 #3
rtilley <rt*****@vt.edu> 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')
Feb 14 '06 #4
On Mon, 13 Feb 2006 19:03:32 +0000 in comp.lang.python, rtilley
<rt*****@vt.edu> 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.
Feb 14 '06 #5
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.
Feb 14 '06 #6
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]
Feb 14 '06 #7
Paul Rubin a écrit :
rtilley <rt*****@vt.edu> 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 !-)
Feb 14 '06 #8
rtilley <rt*****@vt.edu> 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.
Feb 14 '06 #9
> 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.


Feb 14 '06 #10
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.
Feb 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2871
by: Des Small | last post by:
Lately I have found myself using a pattern to make new dictionaries quite often, by which I mean twice: def invert(d): nd = {} ).append(key) for k, v in d] return nd def count(l): d = {}
5
6002
by: Me Padre | last post by:
Hoping someone can help with this. I know I can do this programatically using a loop but I thought there might be some easier or more effective way. I am trying to invert an array. i.e. ...
7
22613
by: tdavidge | last post by:
Folks, Given a table that might contain the rows: DATE ID ==== == 1/1/2001 1A 1/1/2001 2B 1/1/2001 3C 1/1/2001 4D
2
1681
by: osmethod | last post by:
Hello, I'm stuck! I have received help before and progressed greatly from your(Access Group) answers. I need some again please. Thanks in advance for any suggestions. Problem: Take the...
3
25262
by: Peter Aitken | last post by:
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type ushort. I though I would use the complement operator ~ but the compiler won't buy it. How can I do this? Thanks, --...
41
3334
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
1
2874
by: mahesh.nimbalkar | last post by:
Hi, I have color as System.Drawing.Color c1 object as background color. Now I would like to get another System.Drawing.Color c2 object which is invert of c1 color to be used as foreground...
0
2203
by: Karthik | last post by:
Hi, I want to record a sound wave from a mic and at the same time invert it and play the inverted wave.My code goes as follows, however nothing is written into the E:\inverted.wav file.Thanks in...
2
1787
by: Stef Mientki | last post by:
hello, from the manual I read that a bitwise inversion should be done by invert. But from some experiments I see that not works equally well. Is this coincidence ? (The disadvantage of invert...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.