Python程式在句子中用星號替換單詞


在寫作或印刷中使用符號 *(星號)作為參考標記,表示某些字母或單詞已被省略的符號,表示可能但未經證實的語言形式的方式,或用於其他任意目的。

在本文中,我們將討論在Python中用星號替換句子中單詞的不同方法。

輸入-輸出場景

以下是用星號替換句子中單詞的輸入和輸出場景示例:

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

在上面的場景中,我們可以看到句子中的“family”一詞被星號替換了。

使用replace()方法

在給定的字串中,replace()方法查詢作為第一個引數傳遞的字串,並將其替換為第二個引數。

示例

以下是用replace()方法在句子中用星號替換單詞的示例:

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)

輸出

以下是以上程式碼的輸出:

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

使用帶有靜態輸入的for迴圈

以下是使用帶有靜態輸入的for迴圈在句子中用星號替換單詞的方法:

  • 字串應作為靜態輸入提供,並儲存在一個變數中。

  • 在提供靜態輸入後,將可替換的單詞放在另一個變數中。

  • 使用split()函式,從給定的字串建立單詞列表,並將其儲存在名為“word_list”的新變數中。

  • 使用len()函式將星號符號乘以提供的輸入單詞的長度,然後將結果儲存在名為“replaced_word”的新變數中。

  • 使用for迴圈遍歷剛獲得的單詞列表。

  • 利用if條件語句確定迭代器索引處的單詞是否等於輸入單詞。

  • 如果語句為真,則將迭代器索引處的單詞替換為“replaced_word”。

  • 使用join()函式將上述單詞列表轉換為字串。

  • 列印獲得的字串以用星號替換給定輸入句子中的單詞。

  • 程式的退出。

示例

以下是使用帶有靜態輸入的for迴圈在句子中用星號替換單詞的示例:

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)

輸出

以下是以上程式碼的輸出:

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 ********

使用帶有使用者輸入的for迴圈

以下是使用帶有使用者輸入的for迴圈在句子中用星號替換單詞的方法:

  • 使用input()函式從使用者接收字串並將其儲存在一個變數中。

  • 使用input()函式從使用者接收可替換的單詞並將其儲存在另一個變數中。

  • 使用split()方法,從給定的字串建立單詞列表,並將其儲存在名為“word_list”的新變數中。

  • 使用len()函式將星號符號乘以提供的輸入單詞的長度,然後將結果儲存在名為“replaced_word”的新變數中。

  • 使用for迴圈遍歷剛獲得的單詞列表。

  • 利用if條件語句確定迭代器索引處的單詞是否等於輸入單詞。

  • 如果語句為真,則將迭代器索引處的單詞替換為“replaced_word”。

  • 使用join()函式將上述單詞列表轉換為字串。

  • 列印獲得的字串以用星號替換給定輸入句子中的單詞。

  • 程式的退出。

示例

以下是用帶有使用者輸入的for迴圈在句子中用星號替換單詞的示例:

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)

輸出

以下是以上程式碼的輸出:

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 ********

更新時間: 2022年11月23日

3K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.