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