Chopping off spaces at both ends

Discussion in 'Python' started by Madhusudan Singh, Aug 7, 2005.

  1. 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.
     
    Madhusudan Singh, Aug 7, 2005
    #1
    1. Advertising

  2. Madhusudan Singh

    Kay Schluehr Guest

    Use the strip() method.

    Example:

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

    "abc"

    Variants are lstrip() and rstrip().

    Regards,
    Kay
     
    Kay Schluehr, Aug 7, 2005
    #2
    1. Advertising

  3. 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
     
    Bengt Richter, Aug 7, 2005
    #3
  4. Madhusudan Singh

    Tim Roberts Guest

    Madhusudan Singh <> 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.
     
    Tim Roberts, Aug 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rtilley
    Replies:
    22
    Views:
    670
    Christos Georgiou
    Mar 2, 2006
  2. harryajh
    Replies:
    3
    Views:
    438
    Jonathan N. Little
    Mar 12, 2008
  3. Warren Tang
    Replies:
    12
    Views:
    1,079
    Arne Vajhøj
    Jul 24, 2011
  4. Warren Tang
    Replies:
    2
    Views:
    527
    Arne Vajhøj
    Jul 21, 2011
  5. M Yager
    Replies:
    1
    Views:
    142
    Robb Meade
    Jul 2, 2003
Loading...

Share This Page