strip/trim a string

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Mon Apr 22 07:08:14 EDT 2002


On Monday 22 Apr 2002 9:32 am, Bjørn OveGrøtan wrote:
> yes, I'm a newbie at python.
>
> and I need to strip/trim a string... e.g. I have a string of unknown
> length, and only care
> for the last five(5) characters.
>
> An example string:
> xxxNOGBJ  94873
>
> where 94873 is the only part of the string I want to keep


If it is allways the last five then :


s="xxxNOGBJ  94873"

s[-5:]


at the python int.:
$ python
Python 2.0 (#11, Nov  6 2000, 09:01:06)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> s="xxxNOGBJ  94873"
>>> s[-5]
'9'
>>> s[:-5]
'xxxNOGBJ  '
>>> s[-5:]
'94873'
>>>

take a look at:-

http://www.python.org/doc/current/tut/node5.html#SECTION005120000000000000000



>
> best regards
>
> Bjørn Ove





More information about the Python-list mailing list