Python - Word Replacement





Replacing the complete string or a part of string is a very frequent requirement in text processing. The replace() method returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

Following is the syntax for replace() method −

str.replace(old, new[, max])

Parameters

This method returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument max is given, only the first count occurrences are replaced.

Example

The following example shows the usage of replace() method.

str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))

Result

When we run above program, it produces the following result −

Topics You May Be Interested In
Python Basic Syntax Python - Dns Look-up
Python Loops Python - Databases And Sql
Python Strings Python - Sorting Lines
Python - Hash Table Python - Text Wrapping
Python - Heaps Python Tutorial
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Replacement Ignoring Case

import re
sourceline  = re.compile("Tutor", re.IGNORECASE)
 
Replacedline  = sourceline.sub("Tutor","Tutorialspoint has the best tutorials for learning.")
print (Replacedline)

When we run the above program, we get the following output −

Tutorialspoint has the best Tutorials for learning.


Frequently Asked Questions

+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..
+
Ans: Python Text Processing Tutorial for Beginners - Learn Python Text Processing in simple and easy steps starting from basic to advanced concepts with examples including Text Processing,Text Processing Environment,String Immutability,Sorting Lines,Reformatting Paragraphs,Counting Token in Paragraphs ,Convert Binary to ASCII,Convert ASCII to Binary,Strings as Files,Backward File Reading,Filter Duplicate Words,Extract Emails from Text,Extract URL from Text,Pretty Print Numbers,Text Processing State Machine,Capitalize and Translate,Tokenization,Remove Stopwords,Synonyms and Antonyms,Text Translation,Word Replacement,Spelling Check,WordNet Interface,Corpora Access,Tagging Words,Chunks and Chinks,Chunk Classification,Text Classification,Bigrams,Process PDF,Process Word Document,Reading RSS feed,Sentiment Analysis,Search and Match,Text Munging,Text wrapping,Frequency Distribution,Text Summarization,Stemming Algorithms,Constrained Search. view more..




Rating - NAN/5
504 views

Advertisements