Python程式:替換除首字元外的所有指定字元K
Python內建了一些函式,例如slicing()和replace(),可以用來替換除首字元外的所有指定字元K。
在Python中,字串是最常用的資料型別之一。它們可以透過簡單地在引號中包含字元來建立。Python對單引號和雙引號的處理方式相同。為變數賦值和建立字串非常簡單。
示例
假設我們已經獲取了一個輸入字串和字元K。我們將使用上述方法,將輸入字串中除第一個字元外的所有字元K替換為輸入字元。
輸入
inputString = 'blueforblueforblue'
輸出
Resultant string after replacing: bluefor*luefor*lue
使用切片和replace()函式
在這種方法中,我們將結合使用切片和replace函式來替換出現的字元。replace函式返回一個字串的副本,該副本將所有舊子字串的出現替換為另一個新的子字串。
語法
string.replace(old, new, count)
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存輸入字串。
列印輸入字串。
建立另一個變數來儲存用於替換的輸入字元K。
從第二個字元切片到字串的末尾,並將第一個字元的所有出現替換為給定的符號K。
將此結果與第一個字元連線。
列印替換後的結果字串。
示例1
以下程式使用切片和replace()函式返回替換除首字元外的所有指定字元K後的字串。
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # replacing all the characters except the first character with the given k symbol resultantStr = inputString[0] + inputString[1:].replace(inputString[0], k) # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
輸出
執行上述程式將生成以下輸出:
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
示例2
以下程式使用replace()函式返回替換除首字元外的所有指定字元K後的字串。
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # Replacing using the replace() method resultantStr = inputString.replace( inputString[0], k).replace(k, inputString[0], 1) # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
輸出
執行上述程式將生成以下輸出:
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
使用for迴圈
在這種方法中,我們將藉助Python的for迴圈和not運算子來替換除首字元外的所有指定字元K。not運算子是一個邏輯運算子,如果語句/語句不為真,則返回True,否則返回False。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存輸入字串。
列印輸入字串。
建立另一個變數來儲存用於替換的輸入字元K。
初始化一個空字串以儲存結果字串。
使用for迴圈遍歷輸入字串的每個字元。
使用if條件語句檢查當前字元是否等於輸入字串的第一個字元(使用索引),以及它是否不存在於結果字串中(使用not運算子)。
如果條件為真,則使用+(連線)運算子將該當前字元新增到結果字串中。
使用elif條件語句檢查當前字元是否等於輸入字串的第一個字元,以及它是否已存在於結果字串中。
如果條件為真,則使用+運算子將輸入字元K新增到結果字串中。
否則,直接將當前字元新增到結果字串中,無需修改。
列印替換後的結果字串。
示例
以下程式使用for迴圈返回替換除首字元外的所有指定字元K後的字串。
# input string inputString = 'blueforblueforblue' # printing input string print("Input string: ", inputString) # input K character for replacing k = '*' # initilazing empty string for storing resultant string resultantStr= "" # travsering through each character of an input string for c in inputString: # checking whether the current character is equal to the first character # of the input string and it is NOT present in the resultant string if c==inputString[0] and c not in resultantStr: # add that current character to the resultant string resultantStr+=c # checking whether the current character is equal to the first character # of the input string and it is in the resultant string elif c==inputString[0] and c in resultantStr: # adding k to the resultant string resultantStr+=k else: # else adding that character directly without modifying resultantStr+=c # printing the resultant string after replacing print("Resultant string after replacing: ", resultantStr)
輸出
執行上述程式將生成以下輸出:
Input string: blueforblueforblue Resultant string after replacing: bluefor*luefor*lue
結論
在本文中,我們學習瞭如何使用三種不同的方法替換除首字元外的所有指定字元K。我們學習瞭如何使用切片來擷取字串並替換其元素。此外,我們學習瞭如何使用連線運算子(+)連線或組合兩個字串。