python find next occurrence in string code example

Example 1: python replace first occurrence in string

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

Example 2: how to find the last occurrence of a character in a string in python

original_string = "sentence one. sentence two. sentence three"

last_char_index = original_string.rfind(".")
new_string = original_string[:last_char_index] + "," + original_string
print(new_string)
"sentence one. sententce two, sentence three"