{"id":17604,"date":"2021-08-17T20:25:17","date_gmt":"2021-08-17T14:55:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=17604"},"modified":"2021-11-22T18:37:21","modified_gmt":"2021-11-22T13:07:21","slug":"__trashed-2","status":"publish","type":"post","link":"https:\/\/python-programs.com\/__trashed-2\/","title":{"rendered":"Python Program to Replace a Word with Asterisks in a Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Odd Factors of a Number<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to replace a word with asterisks in a given Sentence:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a sentence and the task is to replace a word with asterisks in a given Sentence.<\/p>\nGiven string = \"hello this is btechgeeks btechgeeks\"\r\nGiven word to be replaced = \"btechgeeks\"<\/pre>\n
The given string [ hello this is btechgeeks btechgeeks ] after replacing with a given word with an asterisk :\r\nhello this is ********** **********<\/pre>\n
Given string = \"good morning this is btechgeeks good morning all\"\r\nGiven word to be replaced = \"good\"<\/pre>\n
The given string [ good morning this is btechgeeks good morning all ] after replacing with a given word with an asterisk :\r\n**** morning this is btechgeeks **** morning all<\/pre>\n
Program to Replace a Word with Asterisks in a Sentence in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello this is btechgeeks btechgeeks\"\r\n# Give the replaceable word as static input and store it in another variable.\r\ngvn_wrd = \"btechgeeks\"\r\n# Split the given string into a list of words using the split() function and\r\n# store it in another variable say \"wrd_lst\".\r\nwrd_lst = gvn_str.split()\r\n# Multiply the asterisk symbol with the length of the given input word using\r\n# the len() function and store it in another variable.\r\nreplacd_word = '*' * len(gvn_wrd)\r\n# Loop in the above-obtained word list using the for loop.\r\nfor itr in range(len(wrd_lst)):\r\n # Check if the iterator value is equal to the given input word using the if\r\n # conditional statement.\r\n # check if thw word at the iterator index is equal is given\r\n if wrd_lst[itr] == gvn_wrd:\r\n # if it is truw thwn replce the word t the iterator index with the replaced word\r\n wrd_lst[itr] = replacd_word\r\n# Convert the above-got word list to string using the join() function.\r\nfinl_str = ' '.join(wrd_lst)\r\n# Print the above-obtained string to replace a word with an asterisk in a\r\n# given input sentence.\r\nprint(\"The given string [\", gvn_str,\r\n \"] after replacing with a given word with an asterisk :\")\r\nprint(finl_str)\r\n<\/pre>\n
The given string [ hello this is btechgeeks btechgeeks ] after replacing with a given word with an asterisk :\r\nhello this is ********** **********<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random sentence = \")\r\n# Give the replaceable word as user input using the input() function and store it in another variable.\r\ngvn_wrd = input(\"Enter some random word = \")\r\n# Split the given string into a list of words using the split() function and\r\n# store it in another variable say \"wrd_lst\".\r\nwrd_lst = gvn_str.split()\r\n# Multiply the asterisk symbol with the length of the given input word using\r\n# the len() function and store it in another variable.\r\nreplacd_word = '*' * len(gvn_wrd)\r\n# Loop in the above-obtained word list using the for loop.\r\nfor itr in range(len(wrd_lst)):\r\n # Check if the iterator value is equal to the given input word using the if\r\n # conditional statement.\r\n # check if thw word at the iterator index is equal is given\r\n if wrd_lst[itr] == gvn_wrd:\r\n # if it is truw thwn replce the word t the iterator index with the replaced word\r\n wrd_lst[itr] = replacd_word\r\n# Convert the above-got word list to string using the join() function.\r\nfinl_str = ' '.join(wrd_lst)\r\n# Print the above-obtained string to replace a word with an asterisk in a\r\n# given input sentence.\r\nprint(\"The given string [\", gvn_str,\r\n \"] after replacing with a given word with an asterisk :\")\r\nprint(finl_str)\r\n<\/pre>\n
Enter some random sentence = good morning this is btechgeeks good morning all\r\nEnter some random word = good\r\nThe given string [ good morning this is btechgeeks good morning all ] after replacing with a given word with an asterisk :\r\n**** morning this is btechgeeks **** morning all<\/pre>\n
\n