Python string – replace()

Configurare noua (How To)

Situatie

replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.

Syntax: string.replace(old, new, count)

old – old substring you want to replace.

new – new substring which would replace the old substring.

count – the number of times you want to replace the old substring with the new substring. (Optional)

Solutie

Pasi de urmat

string = “test some test test test test”

# Prints the string by replacing test by Test
print(string.replace(“test”, “Test”))

# Prints the string by replacing only 3 occurrence of test
print(string.replace(“test”, “test2”, 3))

Test some Test Test Test Test
test2 some test2 test2 test test

Tip solutie

Permanent

Voteaza

(8 din 19 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?