So I have a long list of strings in the same format, and I want to find the last "." character in each one, and replace it with ". - ". I've tried using rfind, but I can't seem to utilize it properly to do this.
7 Answers
This should do it
old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]
-
1Thanks so much man. Going to have to study that for a minute... this is utilizing slices, right? Jan 24, 2013 at 7:41
-
@AdamMagyar yes, container[a:b] slices from a up to b-1 index of the container. If 'a' is omitted, then it defaults to 0; if 'b' is omitted it defaults to len(container). The plus operator just concatenates. The rfind function as you pointed out returns the index around which the replacement operation should take place. Jan 24, 2013 at 7:44
To replace from the right:
def replace_right(source, target, replacement, replacements=None):
return replacement.join(source.rsplit(target, replacements))
In use:
>>> replace_right("asd.asd.asd.", ".", ". -", 1)
'asd.asd.asd. -'
-
1I definitely like this solution but having
replacements=None
parameter seems like an error to me because if the parameter is omitted the function will give an error (tried in Python 2.7). I would suggest either remove the default value, set it to -1 (for unlimited replacements) or better make itreplacements=1
(which I think should be the default behaviour for this particular function according to what the OP wants). According to the docs this parameter is optional, but it must be an int if given.– remarkovMay 14, 2016 at 8:05 -
In case anyone wants a one-liner for this:
". -".join("asd.asd.asd.".rsplit(".", 1))
. All you're doing is performing a string split from the right side for 1 occurrence and joining the string again using the replacement. Apr 23, 2020 at 22:16
I would use a regex:
import re
new_list = [re.sub(r"\.(?=[^.]*$)", r". - ", s) for s in old_list]
-
2This is the only answer that works if there's no dot at all. I'd use a lookahead though:
\.(?=[^.]*$)
– georgJan 24, 2013 at 10:18
A one liner would be :
str=str[::-1].replace(".",".-",1)[::-1]
-
1This is wrong. You're reversing the string, replacing it and then reversing it back. You're doing
.replace
on a reversed string. Both of the strings passed toreplace
have to be reversed too. Otherwise when you reverse the string a second time the letters you just inserted will be backwards. You can only use this if you're replacing one letter with one letter, and even then I wouldn't put this in your code incase someone has to change it in the future and starts wondering why a word is written sdrawkcab. Jan 13, 2019 at 18:08
You can use the function below which replaces the first occurrence of the word from right.
def replace_from_right(text: str, original_text: str, new_text: str) -> str:
""" Replace first occurrence of original_text by new_text. """
return text[::-1].replace(original_text[::-1], new_text[::-1], 1)[::-1]
a = "A long string with a . in the middle ending with ."
# if you want to find the index of the last occurrence of any string, In our case we #will find the index of the last occurrence of with
index = a.rfind("with")
# the result will be 44, as index starts from 0.
Naïve approach:
a = "A long string with a . in the middle ending with ."
fchar = '.'
rchar = '. -'
a[::-1].replace(fchar, rchar[::-1], 1)[::-1]
Out[2]: 'A long string with a . in the middle ending with . -'
Aditya Sihag's answer with a single rfind
:
pos = a.rfind('.')
a[:pos] + '. -' + a[pos+1:]
-
This reverses the replacement string too. Other than that, it's a repeat of root's answer, and, as I say there, pretty inefficient. Jan 24, 2013 at 7:39
-
-
-
Just expecting the user to reverse the string literal by hand isn't a great idea - it is prone to mistakes and unclear. Jan 24, 2013 at 7:50
-
@Lattyware Agreed. I've made it a var. (I realise it's an inefficient method, and is not suitable in all cases - your
replace_right
is much nicer)– Alex LJan 24, 2013 at 7:54