How to replace the last (and only last) character in a string?

Discussion in 'Python' started by Johny, May 3, 2007.

  1. Johny

    Johny Guest

    Let's suppose
    s='12345 4343 454'
    How can I replace the last '4' character?
    I tried
    string.replace(s,s[len(s)-1],'r')
    where 'r' should replace the last '4'.
    But it doesn't work.
    Can anyone explain why?

    Thanks
    L.
    Johny, May 3, 2007
    #1
    1. Advertising

  2. Johny

    Guest

    On May 3, 9:27 am, Johny <> wrote:
    > Let's suppose
    > s='12345 4343 454'
    > How can I replace the last '4' character?
    > I tried
    > string.replace(s,s[len(s)-1],'r')
    > where 'r' should replace the last '4'.
    > But it doesn't work.
    > Can anyone explain why?
    >
    > Thanks
    > L.


    I think the reason it's not working is because you're doing it kind of
    backwards. For one thing, the "string" module is deprecated. I would
    do it like this:

    s = s.replace(s[len(s)-1], 'r')

    Although that is kind of hard to read. But it works.

    Mike
    , May 3, 2007
    #2
    1. Advertising

  3. Johny

    Johny Guest

    On May 3, 4:37 pm, wrote:
    > On May 3, 9:27 am, Johny <> wrote:
    >
    > > Let's suppose
    > > s='12345 4343 454'
    > > How can I replace the last '4' character?
    > > I tried
    > > string.replace(s,s[len(s)-1],'r')
    > > where 'r' should replace the last '4'.
    > > But it doesn't work.
    > > Can anyone explain why?

    >
    > > Thanks
    > > L.

    >
    > I think the reason it's not working is because you're doing it kind of
    > backwards. For one thing, the "string" module is deprecated. I would
    > do it like this:
    >
    > s = s.replace(s[len(s)-1], 'r')
    >
    > Although that is kind of hard to read. But it works.
    >
    > Mike



    Mike it does NOT work for me.
    >>> s.replace(s[len(s)-1], 'r')

    '123r5 r3r3 r5r'

    I need only the last character to be replaced
    Johny, May 3, 2007
    #3
  4. In <>, Johny wrote:

    > Let's suppose
    > s='12345 4343 454'
    > How can I replace the last '4' character?
    > I tried
    > string.replace(s,s[len(s)-1],'r')
    > where 'r' should replace the last '4'.
    > But it doesn't work.
    > Can anyone explain why?


    Because you can't change strings. Any function or method that "changes" a
    string returns a new and modified copy. So does the `string.replace()`
    function. And you don't bind the result to a name, so it is "lost".

    This is shorter than using `replace()`:

    In [9]: s = '12345 4343 454'

    In [10]: s = s[:-1] + 'r'

    In [11]: s
    Out[11]: '12345 4343 45r'

    BTW most things in the `string` module are deprecate because they are
    available as methods on string objects.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, May 3, 2007
    #4
  5. Johny

    jay graves Guest

    On May 3, 9:27 am, Johny <> wrote:
    > Let's suppose
    > s='12345 4343 454'
    > How can I replace the last '4' character?
    > I tried
    > string.replace(s,s[len(s)-1],'r')
    > where 'r' should replace the last '4'.
    > But it doesn't work.
    > Can anyone explain why?


    Instead of doing it that way, you should use slicing.

    >>> s='12345 4343 454'
    >>> s = s[:-1] + 'r'
    >>> print s

    12345 4343 45r
    >>>


    See
    http://docs.python.org/tut/node5.html#strings

    HTH.
    Jay Graves
    jay graves, May 3, 2007
    #5
  6. Johny

    Matimus Guest

    On May 3, 7:44 am, Johny <> wrote:
    > On May 3, 4:37 pm, wrote:
    >
    >
    >
    > > On May 3, 9:27 am, Johny <> wrote:

    >
    > > > Let's suppose
    > > > s='12345 4343 454'
    > > > How can I replace the last '4' character?
    > > > I tried
    > > > string.replace(s,s[len(s)-1],'r')
    > > > where 'r' should replace the last '4'.
    > > > But it doesn't work.
    > > > Can anyone explain why?

    >
    > > > Thanks
    > > > L.

    >
    > > I think the reason it's not working is because you're doing it kind of
    > > backwards. For one thing, the "string" module is deprecated. I would
    > > do it like this:

    >
    > > s = s.replace(s[len(s)-1], 'r')

    >
    > > Although that is kind of hard to read. But it works.

    >
    > > Mike

    >
    > Mike it does NOT work for me.>>> s.replace(s[len(s)-1], 'r')
    >
    > '123r5 r3r3 r5r'
    >
    > I need only the last character to be replaced


    Its not working because str.replace:

    [docstring]
    Help on method_descriptor:

    replace(...)
    S.replace (old, new[, count]) -> string

    Return a copy of string S with all occurrences of substring
    old replaced by new. If the optional argument count is
    given, only the first count occurrences are replaced.
    [/docstring]

    Notice the "all occurrences of substring" part. Strings are immutable,
    so there isn't really any replace, either way you are going to be
    creating a new string. So the best way to do what (I think) you want
    to do is this...

    Code:
    [color=blue][color=green][color=darkred]
    >>> s = '12345 4343 454'
    >>> s = s[:-1]+'r'
    >>> s[/color][/color][/color]
    '12345 4343 45r'
    
    Matimus, May 3, 2007
    #6
  7. On 2007-05-03, Johny <> wrote:
    > Let's suppose
    > s='12345 4343 454'
    > How can I replace the last '4' character?


    >>> s = '12345 4343 454'
    >>> s = s[:-1] + 'X'
    >>> s

    '12345 4343 45X'

    --
    Grant Edwards grante Yow! Where's th' DAFFY
    at DUCK EXHIBIT??
    visi.com
    Grant Edwards, May 3, 2007
    #7
  8. Johny

    Dave Borne Guest

    > Let's suppose
    > s='12345 4343 454'
    > How can I replace the last '4' character?


    If the last '4' will not always be the last character in the string,
    you could do:
    'X'.join(s.rsplit('4',1))

    -Dave
    Dave Borne, May 3, 2007
    #8
  9. Johny

    Guest

    On May 3, 9:44 am, Johny <> wrote:
    > On May 3, 4:37 pm, wrote:
    >
    >
    >
    > > On May 3, 9:27 am, Johny <> wrote:

    >
    > > > Let's suppose
    > > > s='12345 4343 454'
    > > > How can I replace the last '4' character?
    > > > I tried
    > > > string.replace(s,s[len(s)-1],'r')
    > > > where 'r' should replace the last '4'.
    > > > But it doesn't work.
    > > > Can anyone explain why?

    >
    > > > Thanks
    > > > L.

    >
    > > I think the reason it's not working is because you're doing it kind of
    > > backwards. For one thing, the "string" module is deprecated. I would
    > > do it like this:

    >
    > > s = s.replace(s[len(s)-1], 'r')

    >
    > > Although that is kind of hard to read. But it works.

    >
    > > Mike

    >
    > Mike it does NOT work for me.>>> s.replace(s[len(s)-1], 'r')
    >
    > '123r5 r3r3 r5r'
    >
    > I need only the last characte



    Yeah...I'm an idiot. Sorry about that. Listen to the other users!

    Mike
    , May 3, 2007
    #9
    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. Daniel Mark
    Replies:
    6
    Views:
    70,715
    Tim Chase
    Sep 28, 2006
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    714
    those who know me have no need of my name
    May 28, 2006
  3. herman
    Replies:
    5
    Views:
    7,578
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. Victor
    Replies:
    3
    Views:
    124
    Victor
    Sep 12, 2003
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    704
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page