By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

May 3 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On May 3, 9:27 am, Johny <pyt...@hope.czwrote:
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 '07 #2

P: n/a
On May 3, 4:37 pm, kyoso...@gmail.com wrote:
On May 3, 9:27 am, Johny <pyt...@hope.czwrote:
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
May 3 '07 #3

P: n/a
In <11**********************@c35g2000hsg.googlegroups .com>, 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
May 3 '07 #4

P: n/a
On May 3, 9:27 am, Johny <pyt...@hope.czwrote:
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
May 3 '07 #5

P: n/a
On May 3, 7:44 am, Johny <pyt...@hope.czwrote:
On May 3, 4:37 pm, kyoso...@gmail.com wrote:
On May 3, 9:27 am, Johny <pyt...@hope.czwrote:
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...

Expand|Select|Wrap|Line Numbers
  1.         
  2.                         
  3.                         
  4.                 >>s = '12345 4343 454'
  5. s = s[:-1]+'r'
  6. s
  7.  
  8. '12345 4343 45r'
  9.  
May 3 '07 #6

P: n/a
On 2007-05-03, Johny <py****@hope.czwrote:
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
May 3 '07 #7

P: n/a
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
May 3 '07 #8

P: n/a
On May 3, 9:44 am, Johny <pyt...@hope.czwrote:
On May 3, 4:37 pm, kyoso...@gmail.com wrote:
On May 3, 9:27 am, Johny <pyt...@hope.czwrote:
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 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.