Python - 將標點符號替換為 K


在本文中,我們將學習如何用字母“k”替換字串中的標點符號。在使用Python進行文字處理或資料清洗任務時,您可能遇到過這個問題,需要用特定字元或任何符號替換標點符號。在這裡,我們將看到用給定文字“k”替換標點符號的各種方法。

讓我們透過下面的例子來了解一下:

string = "Welcome, to, * the website ! aliens"

這裡我們有一個包含標點符號(如 [, * !])的字串,我們想用文字“k”替換它們。因此,替換後的最終字串將是:

string = "WelcomeK toK K the website K aliens"

執行此操作的方法。

方法 1. 使用字串 replace() 方法。

示例

string = "Welcome, to, * the website ! aliens "
print("String with punctuation: ", string)
modified_str = string.replace(",", "K").replace(".", "K").replace("!", "K").replace("*", "K")
print("String after replacing punctuation: ",modified_str)

輸出

String with punctuation: Welcome, to, * the website ! aliens 
String after replacing punctuation: WelcomeK toK K the website K aliens 

解釋

在上面的例子中,我們使用了字串的 replace() 函式,該函式將每個標點符號替換為字母“k”。replace() 函式將用給定的字元“k”替換所有出現的指定標點符號。您可以在這裡觀察到,我們將標點符號指定到 replace 函式中,如果它與字元字串匹配,則它將被替換。

方法 2. 使用正則表示式。

示例

import re
string = "Welcome, to, * the website ! aliens "
print("String with punctuation: ", string)
modified_str = re.sub(r"[^\w\s]", "K", string)
print("String after replacing punctuation: ",modified_str)

輸出

String with punctuation: Welcome, to, * the website ! aliens 
String after replacing punctuation: WelcomeK toK K the website K aliens 

解釋

在上面的例子中,我們使用了正則表示式的 re.sub() 函式來用給定的文字“k”替換標點符號。我們使用正則表示式模式 [^\w\s] 來匹配不是單詞的字串字元,如果我們找到它,我們將用“k”替換該字元。

方法 3. 使用列表推導式和 join() 方法。

示例

import string
string_text = "Welcome, to, * the website ! aliens"
print("String with punctuation: ", string_text)
modified_str =''.join(["K" if char in string.punctuation else char for char in string_text])

print("String after replacing punctuation: ",modified_str)

輸出

String with punctuation: Welcome, to, * the website ! aliens 
String after replacing punctuation: WelcomeK toK K the website K aliens 

解釋

在上面的例子中,我們使用了列表推導式的方法來遍歷文字的每個字元,如果我們找到標點符號,則用“k”替換標點符號。如果我們找不到任何標點符號,我們將保持字元不變。使用 join() 方法,我們將字元組合起來。

方法 4. 使用帶有字元類的正則表示式。

示例

import re
string_text = "Welcome, to, * the website ! aliens"
print("String with punctuation: ", string_text)
modified_str=re.sub(r"[.,!*]", "K", string_text)

print("String after replacing punctuation: ",modified_str)

輸出

String with punctuation: Welcome, to, * the website ! aliens 
String after replacing punctuation: WelcomeK toK K the website K aliens 

解釋

在上面的例子中,我們使用了正則表示式的 re.sub() 函式來用字元“k”替換標點符號。我們在正則表示式中指定了標點符號 [.,!* ]。此標點符號模式匹配字串中的字元,並將標點符號替換為“k”。您可以在這裡觀察到,我們將標點符號指定到表示式中,如果它與字元字串匹配,則它將被替換。

方法 5. 使用帶有 replace() 函式的列表推導式。

示例

import re
string_text = "Welcome, to, * the website ! aliens"
print("String with punctuation: ", string_text)
modified_str = ''.join(["K" if char in [",", ".", "!", "*"] else char for char in string_text])

print("String after replacing punctuation: ",modified_str)

輸出

String with punctuation: Welcome, to, * the website ! aliens 
String after replacing punctuation: WelcomeK toK K the website K aliens 

解釋

在上面的例子中,我們使用了列表推導式來遍歷字串文字的每個字元,如果我們找到標點符號,則用字母“k”替換它。如果標點符號不匹配,則不會替換它。我們使用 [",", ".", "!", "*"] 定義了標點符號,如果您想新增更多標點符號字元,可以將它們新增到此列表中。我們使用 join() 方法組合字元。您可以在 string_text 中看到標點符號,例如 ",", "*", "!", ".",並且在執行操作後,它被替換為 "k"。

因此,我們瞭解了用“k”替換標點符號的方法。我們看到了解決這個問題的各種方法,包括正則表示式、列表推導式和字串方法。我們還添加了自定義標點符號,如果您想替換任何其他標點符號,也可以新增。每種方法都有其獨特的解決問題的方法,您可以根據自己的需要選擇任何適合且簡單的方法。

更新於:2023年10月3日

73 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.