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

2007-05-03 Thread Steven Howe

Dave Borne wrote:

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))
  


   from string import rfind
   def replaceLast_X_with_Y( s, x, y ):
   lastX = s.rfind(x)
   return s[:lastX]+ y + s[lastX+1:]

   s = '12345 4343 454'
   replaceLast_X_with_Y( s, '4', '9' )
   '12345 4343 459'


sph

--
HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

-- 
http://mail.python.org/mailman/listinfo/python-list

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

2007-05-03 Thread kyosohma
On May 3, 9:44 am, Johny <[EMAIL PROTECTED]> wrote:
> On May 3, 4:37 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On May 3, 9:27 am, Johny <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Dave Borne
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Grant Edwards
On 2007-05-03, Johny <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Matimus
On May 3, 7:44 am, Johny <[EMAIL PROTECTED]> wrote:
> On May 3, 4:37 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On May 3, 9:27 am, Johny <[EMAIL PROTECTED]> 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]
>>> s = '12345 4343 454'
>>> s = s[:-1]+'r'
>>> s
'12345 4343 45r'
[/code]

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread jay graves
On May 3, 9:27 am, Johny <[EMAIL PROTECTED]> 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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Johny
On May 3, 4:37 pm, [EMAIL PROTECTED] wrote:
> On May 3, 9:27 am, Johny <[EMAIL PROTECTED]> 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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread kyosohma
On May 3, 9:27 am, Johny <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2007-05-03 Thread Johny
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.

-- 
http://mail.python.org/mailman/listinfo/python-list