Python Program to replace a word with asterisks in a sentence


The use of the symbol * (asterisks) in writing or printing as a reference mark, a sign that some letters or words have been omitted, a way to indicate a possible but unproven linguistic form, or for other arbitrary purposes.

In this article we will discuss different ways to replace a word with asterisks in a sentence in Python.

Input-Output Scenarios

Following is an input and its output scenario of replacing a word in a sentence with asterisks −

Input: Welcome to the TutorialsPoint family.
Output: Welcome to the TutorialsPoint ******

We can see in the above scenario that the word “family” in the sentence is replaced with asterisks.

Using replace() method

In the given string, the replace() method looks for the string passed as the first argument and replaces it with the second argument.

Example

Following is an example to replace a word with asterisks in a sentence using replace() method −

def Asterisk(sentence, word): # Using the replace function to find and replace the word with same no. of asterisks as the word's length. return sentence.replace(word, "*" * len(word)) sentence = "Go feed all the ducks present in the lake" print ('The sentence is :', sentence) censored_sentence = Asterisk(sentence, "ducks") print('Replacement with asterisks is as shown :',censored_sentence)

Output

Following is an output of the above code −

The sentence is : Go feed all the ducks present in the lake
Replacement with asterisks is as shown : Go feed all the ***** present in the lake

Using for loop with static input

Following is the approach to replace a word with asterisks in a sentence using for loop with static input −

  • The string should be provided as static input and saved in a variable.

  • Place the replaceable word in another variable after providing it as static input.

  • Using the split() function, create a list of words from the given string, and save it in a new variable called "word_list."

  • Use the len() function to multiply the asterisk symbol by the length of the provided input word, then store the result in a new variable called "replaced_word."

  • Use the for loop to iterate through the word list you just got.

  • Utilize the if conditional statement to determine whether the word at the iterator index equals the input word.

  • Replace the word at the iterator index with the “replaced_word” if the statement is true.

  • Use the join() function to convert the word list from above to a string.

  • Print the acquired string to replace a word with an asterisk in the given input sentence.

  • The Program's Exit.

Example

Following is an example to replace a word with asterisks in a sentence using for loop with static input −

string = "Replacing the word in the sentence with asterisks for that sentence" # word to be replaced word = "sentence" # splitting the string into list and storing it in another variable word_list = string.split() # Multiplying the asterisk symbol with length of the given word replaced_word = '*' * len(word) for iterator in range(len(word_list)): # checking if the word at the iterator index given is equal if word_list[iterator] == word: word_list[iterator] = replaced_word # Converting the word list to string final_string = ' '.join(word_list) print("The sentence is : ", string) print("Replacement with asterisks is as shown : ",final_string)

Output

Following is an output of the above code −

The sentence is :  Replacing the word in the sentence with asterisks for that sentence
Replacement with asterisks is as shown :  Replacing the word in the ******** with asterisks for that ********

Using for loop with user input

Following is the approach to replace a word with asterisks in a sentence using for loop with user input −

  • Use the input() function to receive the string from the user and store it in a variable.

  • Use the input() function to receive the replaceable word from the user and store it in a different variable.

  • Using the split() method, create a list of words from the given string, and save it in a new variable called "word_list."

  • Use the len() function to multiply the asterisk symbol by the length of the provided input word, then store the result in a new variable called "replaced_word."

  • Use the for loop to iterate through the word list you just got.

  • Utilize the if conditional statement to determine whether the word at the iterator index equals the input word.

  • Replace the word at the iterator index with the “replaced_word” if the statement is true.

  • Use the join() function to convert the word list from above to a string.

  • Print the acquired string to replace a word with an asterisk in the given input sentence.

  • The Program's Exit.

Example

Following is an example to replace a word with asterisks in a sentence using for loop with user input −

string = input("Enter the sentence = ") word = input("Enter the word to be replaced = ") word_list = string.split() replaced_word = '*' * len(word) for itr in range(len(word_list)): if word_list[itr] == word: word_list[itr] = replaced_word final_string = ' '.join(word_list) print("The sentence is : ", string) print("Replacement with asterisks is as shown : ",final_string)

Output

Following is an output of the above code −

Enter the sentence = Replacing the word in the sentence with asterisks for that sentence
Enter the word to be replaced = sentence
The sentence is : Replacing the word in the sentence with asterisks for that sentence
Replacement with asterisks is as shown : Replacing the word in the ******** with asterisks for that ********

Updated on: 23-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements