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

J

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.
 
K

kyosohma

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
 
J

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.

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.
'123r5 r3r3 r5r'

I need only the last character to be replaced
 
M

Marc 'BlackJack' Rintsch

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
 
M

Matimus

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 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:
[QUOTE][QUOTE][QUOTE]
s = '12345 4343 454'
s = s[:-1]+'r'
s[/QUOTE][/QUOTE][/QUOTE]
'12345 4343 45r'
 
D

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
 
K

kyosohma

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top