How to trim n characters from right end of a string?

Discussion in 'Python' started by Robert, Jun 27, 2004.

  1. Robert

    Robert Guest

    Please tell this newbie how to remove n characters from the right end of a
    string.

    Thanks, Robert
    Robert, Jun 27, 2004
    #1
    1. Advertising

  2. Robert

    Paul Bissex Guest

    On Sat, 26 Jun 2004 23:45:42 GMT, Robert <> wrote:
    >
    > Please tell this newbie how to remove n characters from the right end of a
    > string.


    What Tony said, plus this:

    >>> s = "chopme"
    >>> n = 2
    >>> s = s[:-n]
    >>> print s

    chop



    --
    paul bissex, e-scribe.com -- database-driven web development
    413.585.8095
    69.55.225.29
    01061-0847
    72°39'71"W 42°19'42"N
    Paul Bissex, Jun 27, 2004
    #2
    1. Advertising

  3. Robert

    Paul McGuire Guest

    "Robert" <> wrote in message
    news:GUnDc.13670$...
    > Please tell this newbie how to remove n characters from the right end of a
    > string.
    >
    > Thanks, Robert
    >
    >

    At the Python '>>>' prompt:

    >>> a = "This is a string."
    >>> print a[:-5]

    This is a st
    Paul McGuire, Jun 27, 2004
    #3
  4. Robert

    Robert Guest

    "Paul McGuire" <._bogus_.com> wrote in message
    news:ydoDc.13674$...
    > "Robert" <> wrote in message
    > news:GUnDc.13670$...
    > > Please tell this newbie how to remove n characters from the right end of

    a
    > > string.
    > >
    > > Thanks, Robert
    > >
    > >

    > At the Python '>>>' prompt:
    >
    > >>> a = "This is a string."
    > >>> print a[:-5]

    > This is a st
    >
    >


    Got it! Thanks Tony and Paul!

    Robert
    Robert, Jun 27, 2004
    #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. FAQ server
    Replies:
    0
    Views:
    134
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    129
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    208
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    285
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    127
    -Lost
    Apr 24, 2007
Loading...

Share This Page