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.