>>> def rreplace(s, old, new, occurrence):
... li = s.rsplit(old, occurrence)
... return new.join(li)
...
>>> s
'1232425'
>>> rreplace(s, '2', ' ', 2)
'123 4 5'
>>> rreplace(s, '2', ' ', 3)
'1 3 4 5'
>>> rreplace(s, '2', ' ', 4)
'1 3 4 5'
>>> rreplace(s, '2', ' ', 0)
'1232425'
More Related Contents:
- ‘str’ object is not callable Python
- Unexpected returned variable in Python [duplicate]
- How to read a file line-by-line into a list?
- How to check if type of a variable is string?
- Iterating each character in a string using Python
- Extracting date from a string in Python
- Why are empty strings returned in split() results?
- Check if string ends with one of the strings from a list
- How to check if a string only contains letters?
- Replace nth occurrence of substring in string
- How to extract an IP address from an HTML string?
- Regex for existence of some words whose order doesn’t matter
- Remove ‘b’ character do in front of a string literal in Python 3 [duplicate]
- How to test if one string is a subsequence of another? [duplicate]
- How would I get everything before a : in a string Python
- Python regex: splitting on pattern match that is an empty string
- What is the simplest way to swap each pair of adjoining chars in a string with Python?
- Why python’s list slicing doesn’t produce index out of bound error? [duplicate]
- How to explain the str.maketrans function in Python 3.6?
- How to remove symbols from a string with Python? [duplicate]
- Detecting Vowels vs Consonants In Python [duplicate]
- Why does Python not perform type conversion when concatenating strings?
- How to convert a string with comma-delimited items to a list in Python?
- How to read user input until EOF?
- How to determine whether a substring is in a different string [duplicate]
- String concatenation vs. string substitution in Python
- Byte Array to Hex String
- How do I format a number with a variable number of digits in Python?
- Column of lists, convert list to string as a new column
- How do I convert a list of ascii values to a string in python?