Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Chopping off spaces at both ends

Reply
Thread Tools

Chopping off spaces at both ends

 
 
Madhusudan Singh
Guest
Posts: n/a
 
      08-07-2005
Hi

I am a newbie to python and am using it to interface some of my lab
equipment.

How does one get rid of spaces at both ends of a string ? A little like the
trim() intrinsic in fortran 95.

One of my instruments is returning a string that has one or more blanks in
it, and that is complicating string matching tests in one of my functions.

Thanks.
 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      08-07-2005
Use the strip() method.

Example:

>>> "\t abc \n".strip()

"abc"

Variants are lstrip() and rstrip().

Regards,
Kay

 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      08-07-2005
On 7 Aug 2005 10:14:33 -0700, "Kay Schluehr" <> wrote:

>Use the strip() method.
>
>Example:
>
>>>> "\t abc \n".strip()

>"abc"
>
>Variants are lstrip() and rstrip().
>


and also occasionally useful:

>>> 'abc123cab'.strip('bca')

'123'

I.e., a strip argument as an unordered set of characters that
causes stripping so long as characters at the end(s) of the
string being stripped are found that are in the set.

Regards,
Bengt Richter
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      08-09-2005
Madhusudan Singh <spammers-go-> wrote:
>
> I am a newbie to python and am using it to interface some of my lab
>equipment.
>
> How does one get rid of spaces at both ends of a string ? A little like the
>trim() intrinsic in fortran 95.
>
> One of my instruments is returning a string that has one or more blanks in
>it, and that is complicating string matching tests in one of my functions.


>>> a = ' abc '
>>> print a.strip()

'abc'
--
- Tim Roberts,
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Support for both Web and Desktop front-ends [Architecture] Warren Tang Java 12 07-24-2011 01:37 AM
Re: Support for both Web and Desktop front-ends [Architecture] Warren Tang Java 2 07-21-2011 10:43 PM
Select box chopping off content problem? harryajh HTML 3 03-12-2008 03:24 PM
spaces at ends of filenames or directory names on Win32 rtilley Python 22 03-02-2006 10:31 AM
Chopping up a string of characters? Cavello C++ 3 01-23-2004 05:56 PM



Advertisments