Replace First Occurrence of String in Python

Replace first occurrence of string in Python

string replace() function perfectly solves this problem:

string.replace(s, old, new[, maxreplace])

Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

>>> u'longlongTESTstringTEST'.replace('TEST', '?', 1)
u'longlong?stringTEST'

How can I replace the first occurrence of a character in every word?

I would do a regex replacement on the following pattern:

@(@*)

And then just replace with the first capture group, which is all continous @ symbols, minus one.

This should capture every @ occurring at the start of each word, be that word at the beginning, middle, or end of the string.

inp = "hello @jon i am @@here or @@@there and want some@thing in '@here"
out = re.sub(r"@(@*)", '\\1', inp)
print(out)

This prints:

hello jon i am @here or @@there and want something in 'here

Replace all except the first occurrence of a substring in Python?

With additional "reversed" substitution step:

s = "SELECT sdfdsf SELECT sdrrr SELECT 5445ff"
res = s.replace("SELECT", "@@@SELECT").replace("@@@SELECT", "SELECT", 1)
print(res)

The output:

SELECT sdfdsf @@@SELECT sdrrr @@@SELECT 5445ff

A more sophisticated, but ensuring target word boundaries, could be as below:

import re

def make_replacer():
rpl = ''
def inner(m):
nonlocal rpl
res = rpl + m.group()
rpl = '@@@'
return res
return inner

s = "SELECT sdfdsf SELECT sdrrr SELECT 5445ff"
res = re.sub(r'\bSELECT\b', make_replacer(), s)
print(res) # SELECT sdfdsf @@@SELECT sdrrr @@@SELECT 5445ff

replace the first occurrence of a string element in list

since you want x number of occurrence to be replaced, the replace function offers this string.replace(s, old, new[, maxreplace]), you might want to pass on b which is the maxreplace as the third parameter.

a, b = input().split()
a = int(a)
d=[]
for x in range(1,a+1):
globals()['side%s' % x] = input("Enter something: ")
d.append(globals()['side%s' % x])

d = [s.replace('1', '0', int(b)) for s in d] # maxreplace will be b only

print(d)

Replace the first occurrence of a pattern in a String

Use replacen

Replaces first N matches of a pattern with another string.

replacen creates a new String, and copies the data from this string
slice into it. While doing so, it attempts to find matches of a
pattern. If it finds any, it replaces them with the replacement string
slice at most count times.

let input = "Life is Life".to_string();
let output = input.replacen("Life", "My wife", 1);

assert_eq!("My wife is Life", output);

Rust Playground

Replace first occurrence of character in string, but only if first character

There is an lstrip method just for that:

str.lstrip([chars])

Return a copy of the string with leading characters removed. The chars
argument is a string specifying the set of characters to be removed.

'/test/test.json'.lstrip('/')

Ouput:

'test/test.json'


Related Topics



Leave a reply



Submit