Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to replace a word with asterisks in a sentence
Asterisks (*) are commonly used in text processing to censor or hide sensitive words by replacing them with symbols. In Python, we can replace specific words with asterisks using several approaches.
In this article, we will explore different methods to replace a word with asterisks in a sentence.
Input-Output Example
Here's what word replacement with asterisks looks like ?
Input: Welcome to the TutorialsPoint family Output: Welcome to the TutorialsPoint ******
The word "family" is replaced with six asterisks (matching its length).
Using replace() Method
The replace() method searches for a substring and replaces all occurrences with a new string. We can combine it with len() to create asterisks matching the word's length.
Example
def replace_with_asterisks(sentence, word):
# Replace word with asterisks matching its length
return sentence.replace(word, "*" * len(word))
sentence = "Go feed all the ducks present in the lake"
print('Original sentence:', sentence)
censored_sentence = replace_with_asterisks(sentence, "ducks")
print('After replacement:', censored_sentence)
Original sentence: Go feed all the ducks present in the lake After replacement: Go feed all the ***** present in the lake
Using For Loop with List Processing
This approach splits the sentence into words, iterates through each word, and replaces matches with asterisks.
Example
sentence = "Replacing the word in the sentence with asterisks for that sentence"
target_word = "sentence"
# Split sentence into words
word_list = sentence.split()
# Create replacement asterisks
replacement = '*' * len(target_word)
# Replace matching words
for i in range(len(word_list)):
if word_list[i] == target_word:
word_list[i] = replacement
# Join words back into sentence
result = ' '.join(word_list)
print("Original:", sentence)
print("Modified:", result)
Original: Replacing the word in the sentence with asterisks for that sentence Modified: Replacing the word in the ******** with asterisks for that ********
Using List Comprehension
A more concise approach using list comprehension to replace words ?
Example
def censor_word(sentence, word):
words = sentence.split()
asterisks = '*' * len(word)
censored_words = [asterisks if w == word else w for w in words]
return ' '.join(censored_words)
text = "Python is a great programming language and Python is easy to learn"
result = censor_word(text, "Python")
print("Original:", text)
print("Censored:", result)
Original: Python is a great programming language and Python is easy to learn Censored: ****** is a great programming language and ****** is easy to learn
Case-Insensitive Replacement
To handle different cases of the same word, we can make the replacement case-insensitive ?
Example
import re
def censor_case_insensitive(sentence, word):
# Create pattern for case-insensitive matching
pattern = re.compile(re.escape(word), re.IGNORECASE)
replacement = '*' * len(word)
return pattern.sub(replacement, sentence)
text = "Python programming and python scripting with PYTHON"
result = censor_case_insensitive(text, "python")
print("Original:", text)
print("Censored:", result)
Original: Python programming and python scripting with PYTHON Censored: ****** programming and ****** scripting with ******
Comparison of Methods
| Method | Case Sensitive | Performance | Best For |
|---|---|---|---|
replace() |
Yes | Fastest | Simple exact matches |
| For loop | Yes | Medium | Word-by-word processing |
| List comprehension | Yes | Medium | Concise, readable code |
| Regular expressions | Optional | Slower | Complex pattern matching |
Conclusion
Use replace() for simple word censoring, list comprehension for readable code, and regular expressions for case-insensitive or pattern-based replacements. All methods preserve the original word length with matching asterisks.
