- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to replace a word with asterisks in a sentence
We can use a program to replace a word with asterisks in a sentence to censor swear words, etc from a sentence. For example,
If we have a sentence,
"Go feed all the ducks in the lake"
And a word that we want to replace with asterisks, let's say ducks. Then our final sentence will look like −
"Go feed all the ***** in the lake"
We can directly use the replace function from python to achieve this functionality. For example,
def replaceWithAsterisk(sent, word): # Use the replace function to find and replace the word with # same number of asterisks as the length of the word. return sent.replace(word, "*" * len(word)) sent = "Go feed all the ducks in the lake" censored_sent = replaceWithAsterisk(sent, "ducks") print(censored_sent)
This will give the output −
Go feed all the ***** in the lake
- Java Program to replace a word with asterisks in a sentence
- C# Program to replace a character with asterisks in a sentence
- PHP program to replace a word with a different symbol in a sentence
- Python program to reverse each word in a sentence?
- Java program to reverse each word in a sentence
- Python program to remove all duplicates word from a given sentence.
- C program to Replace a word in a text by another given word
- PHP program to find the first word of a sentence
- Print longest palindrome word in a sentence in C Program
- C++ program for length of the longest word in a sentence
- How to replace all occurrences of a word in a string with another word in java?
- Java program to count the characters in each word in a given sentence
- Python program to count words in a sentence
- Python program to sort Palindrome Words in a Sentence