TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Replace Example

Use the replace method to change a substring in a string into another substring.
Replace. A string cannot be changed in-place. We cannot assign characters. Instead we use the replace method to create a new string.Strings
With this method, we specify a substring that we want to replace, and another we want to replace it with. We can use an optional second argument.
First example. Let us begin with this simple example. Replace accepts two substring arguments: the "before" and "after" parts that are replaced.

And: The third argument is optional. It is a count. It indicates the maximum number of instances to replace.

Tip: Replace only handles substrings, not characters. If you need to replace many single characters, please consider the translate method.

Translate
Python program that uses replace value = "aabc" # Replace a substring with another. result = value.replace("bc", "yz") print(result) # Replace the first occurrence with a substring. result = value.replace("a", "x", 1) print(result) Output aayz xabc
Replace count. The third argument to replace() is optional. If we specify it, the replace method replaces that number of occurrences—this is a count argument.

Here: We have a string that says "cat cat cat" and we replace all occurrences, 0 occurrences, and 1 and 2 occurrences.

Python program that uses replace, count argument before = "cat cat cat" print(before) # Replace all occurrences. after = before.replace("cat", "bird") print(after) # Replace zero occurrences. # ... This makes no sense. after = before.replace("cat", "bird", 0) print(after) # Replace 1 occurrence. after = before.replace("cat", "bird", 1) print(after) # Replace first 2 occurrences. after = before.replace("cat", "bird", 2) print(after) Output cat cat cat bird bird bird cat cat cat bird cat cat bird bird cat
A note. Replace() is needed to change a part of string to a new string. If the index of the match is known, we could use two slices and then construct a new string with concatenation.Substring
A summary. With replace() we copy a string. The original is not changed. The copied string has the specified number of substrings replaced with a new substring (if found).
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf