Replace word in file python: In the previous article, we have discussed Python Program to Find Sum of Odd Factors of a Number
Given a sentence and the task is to replace a word with asterisks in a given Sentence.
Examples:
Example1:
Input:
Given string = "hello this is btechgeeks btechgeeks" Given word to be replaced = "btechgeeks"
Output:
The given string [ hello this is btechgeeks btechgeeks ] after replacing with a given word with an asterisk : hello this is ********** **********
Example2:
Input:
Given string = "good morning this is btechgeeks good morning all" Given word to be replaced = "good"
Output:
The given string [ good morning this is btechgeeks good morning all ] after replacing with a given word with an asterisk : **** morning this is btechgeeks **** morning all
- Python Program for Snake Case of a Given Sentence
- Python Program to Remove the last Word from the String/Sentence
- Python Program to Sort words in Alphabetic Order
Program to Replace a Word with Asterisks in a Sentence in Python
Below are the ways to replace a word with asterisks in a given Sentence:
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Give the replaceable word as static input and store it in another variable.
- Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
- Multiply the asterisk symbol with the length of the given input word using the len() function and store it in another variable say “replaced_word”.
- Loop in the above-obtained word list using the for loop.
- Check if the word at the iterator index is equal is given input word using the if conditional statement.
- If the statement is true, replace the word at the iterator index with the replaced_word.
- Convert the above-got word list to string using the join() function.
- Print the above-obtained string to replace a word with an asterisk in a given input sentence.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello this is btechgeeks btechgeeks" # Give the replaceable word as static input and store it in another variable. gvn_wrd = "btechgeeks" # Split the given string into a list of words using the split() function and # store it in another variable say "wrd_lst". wrd_lst = gvn_str.split() # Multiply the asterisk symbol with the length of the given input word using # the len() function and store it in another variable. replacd_word = '*' * len(gvn_wrd) # Loop in the above-obtained word list using the for loop. for itr in range(len(wrd_lst)): # Check if the iterator value is equal to the given input word using the if # conditional statement. # check if thw word at the iterator index is equal is given if wrd_lst[itr] == gvn_wrd: # if it is truw thwn replce the word t the iterator index with the replaced word wrd_lst[itr] = replacd_word # Convert the above-got word list to string using the join() function. finl_str = ' '.join(wrd_lst) # Print the above-obtained string to replace a word with an asterisk in a # given input sentence. print("The given string [", gvn_str, "] after replacing with a given word with an asterisk :") print(finl_str)
Output:
The given string [ hello this is btechgeeks btechgeeks ] after replacing with a given word with an asterisk : hello this is ********** **********
Method #2: Using For loop (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the replaceable word as user input using the input() function and store it in another variable.
- Split the given string into a list of words using the split() function and store it in another variable say “wrd_lst”.
- Multiply the asterisk symbol with the length of the given input word using the len() function and store it in another variable say “replaced_word”.
- Loop in the above-obtained word list using the for loop.
- Check if the word at the iterator index is equal is given input word using the if conditional statement.
- If the statement is true, replace the word at the iterator index with the replaced_word.
- Convert the above-got word list to string using the join() function.
- Print the above-obtained string to replace a word with an asterisk in a given input sentence.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some random sentence = ") # Give the replaceable word as user input using the input() function and store it in another variable. gvn_wrd = input("Enter some random word = ") # Split the given string into a list of words using the split() function and # store it in another variable say "wrd_lst". wrd_lst = gvn_str.split() # Multiply the asterisk symbol with the length of the given input word using # the len() function and store it in another variable. replacd_word = '*' * len(gvn_wrd) # Loop in the above-obtained word list using the for loop. for itr in range(len(wrd_lst)): # Check if the iterator value is equal to the given input word using the if # conditional statement. # check if thw word at the iterator index is equal is given if wrd_lst[itr] == gvn_wrd: # if it is truw thwn replce the word t the iterator index with the replaced word wrd_lst[itr] = replacd_word # Convert the above-got word list to string using the join() function. finl_str = ' '.join(wrd_lst) # Print the above-obtained string to replace a word with an asterisk in a # given input sentence. print("The given string [", gvn_str, "] after replacing with a given word with an asterisk :") print(finl_str)
Output:
Enter some random sentence = good morning this is btechgeeks good morning all Enter some random word = good The given string [ good morning this is btechgeeks good morning all ] after replacing with a given word with an asterisk : **** morning this is btechgeeks **** morning all
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.