Python 程式將字串中重複的字元大寫
在本文中,我們將學習如何在 Python 中將字串中重複的字元大寫。
使用的方法
以下是完成此任務的各種方法 -
使用字典雜湊
使用 count() 函式
使用 replace() 和 len() 函式
使用 Counter() 函式
示例
假設我們已經獲取了一個包含一些隨機文字的輸入字串。我們現在將使用上述方法將輸入字串中重複的字元轉換為大寫。
輸入
inputString = 'hello tutorialspoint'
輸出
heLLO TuTOrIaLspOInT
在上面的輸入字串中,字元l、o、t、i是重複的。因此,它們被轉換為大寫/大寫。
方法 1:使用字典雜湊
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
建立一個函式RepeatedCharToUpper(),該函式透過接受輸入字串作為引數,將字串中重複的字元返回為大寫。
建立一個空字典來儲存字串字元頻率。
使用for 迴圈遍歷輸入字串的每個字元。
使用if 條件語句檢查當前字元是否已存在於上面建立的新字典中。
如果條件為真,則將字元的頻率/計數增加 1
否則,將此字元新增到字典中,值為 1。
再次使用 for 迴圈遍歷輸入字串的每個字元。
使用if 條件語句檢查當前字元的頻率是否大於 1(如果計數>1,則表示重複)。
使用upper() 函式將字元更改為大寫。
將該當前字元新增到結果字串中。
返回結果字串。
建立一個變數來儲存輸入字串。
透過將輸入字串傳遞給它來呼叫上面定義的RepeatedCharToUpper()函式並列印結果。
示例
以下程式使用字典雜湊將字串中所有重複的字元轉換為大寫後返回一個字串 -
# function to change the repeated characters in a string to uppercase
# by accepting the input string as an argument
def RepeatedCharToUpper(inputString):
# Creating an empty dictionary to store characters with their frequencies
newDict = {}
# traversing through each character of an input string
for c in inputString:
# checking whether the character is present in the above dictionary
if c in newDict:
# Incrementing the frequency/count of character by 1
newDict[c] = newDict[c]+1
# Else insert this character in the dictionary
else:
newDict[c] = 1
# Taking a variable to store the string
res = ''
# traversing through each character of an input string
for c in inputString:
# checking if character frequency is greater than 1(repeated character)
if newDict[c] > 1:
# As it is repeated so changing this character into uppercase
c = c.upper()
# adding each character to the resultant string
res = res+c
# returning the resultant string
return res
# input string
inputString = 'hello tutorialspoint'
# calling the above defined RepeatedCharToUpper() function
# by passing input string to it
print(RepeatedCharToUpper(inputString))
輸出
執行後,上述程式將生成以下輸出 -
heLLO TuTOrIaLspOInT
方法 2:使用 count() 函式
字串 count() 函式
返回給定值(字元)在字串中出現的次數。
語法
string.count(value, start, end)
示例
以下程式使用 count() 函式將字串中所有重複的字元轉換為大寫後返回一個字串 -
# input string
inputString = 'hello tutorialspoint'
# empty string for storing resultant string
res = ""
# traversing through each character of an input string
for c in inputString:
# checking whether the current character is not space and # its frequency is greater than 1
if(c != "" and inputString.count(c) > 1):
# converting to uppercase if the condition
# and adding it to the resultant string
res += c.upper()
else:
# else adding that current character to the resultant string without modifying
res += c
print("Resultant string after capitalizing repeated characters:\n", res)
輸出
執行後,上述程式將生成以下輸出 -
Resultant string after capitalizing repeated characters: heLLO TuTOrIaLspOInT
方法 3:使用 replace() 和 len() 函式
len() 函式 - len() 方法返回物件中的專案數。當物件是字串時,len() 函式返回字串中的字元數。
replace() 函式 - 返回字串的副本,該副本用另一個新子字串替換舊子字串的所有出現。
語法
string.replace(old, new, count)
示例
以下程式使用 replace() 和 len() 函式將字串中所有重複的字元轉換為大寫後返回一個字串 -
# input string
inputString = 'hello tutorialspoint'
# getting string length
stringLength = len(inputString)
# empty string for storing resultant string
res = ""
# traversing through each character of an input string
for c in inputString:
# replacing the current character with space
k = inputString.replace(c, "")
if(len(k) != stringLength-1):
res += c.upper()
else:
res += c
print("Resultant string after capitalizing repeated characters:\n", res)
輸出
執行後,上述程式將生成以下輸出 –
Resultant string after capitalizing repeated characters: heLLO TuTOrIaLspOInT
方法 4:使用 Counter() 函式
Counter() 函式 - 一個計數可雜湊物件的子類。它在被呼叫/呼叫時隱式地建立可迭代物件的雜湊表。
這裡它返回字串字元的頻率作為鍵值對。
示例
以下程式使用 Counter() 函式將字串中所有重複的字元轉換為大寫後返回一個字串 -
# importing Counter from the collections module
from collections import Counter
# input string
inputString = 'hello tutorialspoint'
# empty string for storing resultant string
res = ""
# getting the frequency of characters as a dictionary
frequency = Counter(inputString)
# traversing through each character of an input string
for c in inputString:
# checking whether the current character is not space and its frequency is greater than 1
if(c != "" and frequency[c] > 1):
res += c.upper()
else:
res += c
print("Resultant string after capitalizing repeated characters:\n", res)
輸出
Resultant string after capitalizing repeated characters: heLLO TuTOrIaLspOInT
結論
在這篇文章中,我們學習了 4 種不同的方法來將字串中重複的字元大寫。我們還了解了如何使用字典雜湊獲取任何可迭代物件的頻率。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP