Help with String Manipulation

Doug Stanfield DOUGS at oceanic.com
Wed Jun 23 03:06:25 CEST 1999


If you're new to Python it might seem like some magic in this:

Python 1.5.1 (#1, Sep  3 1998, 22:51:17)  [GCC 2.7.2.3] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> testring = "This is an example"
>>> testlist = map (ord, testring)
>>> testlist
[84, 104, 105, 115, 32, 105, 115, 32, 97, 110, 32, 101, 120, 97, 109, 112,
108, 101]
>>> testlist.reverse()
>>> testlist
[101, 108, 112, 109, 97, 120, 101, 32, 110, 97, 32, 115, 105, 32, 115, 105,
104, 84]
>>> import array
>>> finalstring = array.array('b', testlist).tostring()
>>> finalstring
'elpmaxe na si sihT'
>>>

A string is immutable thus the conversion to a list.  The reverse() function
does so in place and the array magic (ref
http://www.python.org/doc/essays/list2str.html for other ways to do it)
gives back a string.  Function creation is left as an exercise...

-Doug-

> -----Original Message-----
> From: Samuel G. Williams [mailto:samw at masu.wwa.com]
> Sent: Tuesday, June 22, 1999 1:22 PM
> To: python-list at cwi.nl
> Subject: Help with String Manipulation
> 
> 
> I know this is a very stupid question and one that has been addressed
> within available documentation, however I need a quick hand. 
> I am in need
> of a routine to reverse the order of a string. I have tried 
> several things
> that have resulted in failures. It is really stupid, but I am just
> learning python. I can do this readily in C, perl, tcl, but 
> am at a loss.
> I would appreciate any help I can get.
> 
> Thanks in advance.
> 
> -- 
> 
> 
> Sam Williams  wb5yni					samurai at acm.org
> Senior IS Staff Specialist			        samw at wwa.com
> --------------------------------------------------------------
> -------------
> 
> 	Thinking of running your critical apps on NT?
> 
> 		Isn't there enough world suffering?
> 




More information about the Python-list mailing list