바꿀 횟수는 꼭 써주지 않아도 된다.
replace(old, new, [count]) -> replace("찾을값", "바꿀값", [바꿀횟수])
text = '123,456,789,999'
,을 하나만 지우고 싶다면 count자리에 1을 다지우고 싶다면 비워두면 된다.
replace_t1 = text.replace(",", "",1)
def replaceRight(original, old, new, count_right):
repeat=0
text = original
old_len = len(old)
count_find = original.count(old)
if count_right > count_find : # 바꿀 횟수가 문자열에 포함된 old보다 많다면
repeat = count_find # 문자열에 포함된 old의 모든 개수(count_find)만큼 교체한다
else :
repeat = count_right # 아니라면 입력받은 개수(count)만큼 교체한다
while(repeat):
find_index = text.rfind(old) # 오른쪽부터 index를 찾기위해 rfind 사용
text = text[:find_index] + new + text[find_index+old_len:]
repeat -= 1
return text