Python程序随机将字符串中的字符大写
在这篇文章中,我们将学习如何使用python随机将字符串中的字符转换为大写。
使用的方法
以下是完成此任务的各种方法 -
使用join()、choice()、lower()、upper()函数。
使用random.randInt()和random.sample()函数。
使用map()、choice()、zip()函数
示例
假设我们已经获取了一个输入字符串。我们现在将使用上述方法随机将输入字符串的字符转换为大写。
输入
inputString = 'tutorialspoint'
输出
String after converting chars lower or uppercase randomly: tUToriaLsPOINT
在这个例子中,我们将输入字符串'tutorialspoint'的字符随机转换为大写。
方法1:使用join()、choice()、lower、upper函数
lower - 将字符串中所有大写字符转换为小写字符。
random.choice()函数(从序列(如列表、元组、数字范围)中随机选择一个项目)。
upper - 将字符串中所有小写字符转换为大写字符。
join() - join()是Python中的一个字符串函数,用于连接由字符串分隔符分隔的序列元素。此函数连接序列元素以转换为字符串。
算法(步骤)
以下是执行所需任务应遵循的算法/步骤 -。
使用import关键字从random模块导入choice函数。
创建一个变量来存储输入字符串。
打印输入字符串。
遍历输入字符串的每个字符,并使用random.choice()随机选择大写或小写字符。这里使用join将结果转换为字符串
随机将输入字符串的字符转换为大写后,打印结果字符串
示例
以下程序使用join()、random.choice()、lower和upper函数返回随机将输入字符串的字符转换为大写后的字符串 -
# importing choice from the random module
from random import choice
# input string
inputString = 'tutorialspoint'
# printing input string
print("Input String:", inputString)
# traversing through each character of a string and selecting
# either upper or lowercase char randomly using choice()
# here join is used to convert to a string
randchars_str = ''.join(choice((str.upper, str.lower))(char) for char in inputString)
# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", randchars_str)
输出
执行上述程序后,将生成以下输出 -
Input String: tutorialspoint String after converting chars lower or uppercase randomly: tUTORIAlSPOInT
方法2:使用random.randInt()和random.sample()函数
random.randint()方法:返回指定范围内的随机数。
random.sample()方法:它返回一个列表,其中包含从序列中随机选择的指定数量的元素。
语法
random.sample(sequence, k)
算法(步骤)
以下是执行所需任务应遵循的算法/步骤 -
使用list()函数将给定的输入字符串转换为列表。
使用random.randInt()函数计算需要转换为大写的随机索引数。
通过将列表长度和所需元素数作为参数传递给random.sample()函数,从0到列表长度获取上述n个随机元素。
将上述索引保存到一个列表中,例如randomIndexList。
使用for循环遍历上述randomIndexList。
使用upper()函数将该索引处的字符串元素转换为大写。
连接所有列表元素,并使用join()函数转换为字符串。
打印结果。
示例
以下程序返回一个字符串,该字符串使用以下方法随机将输入字符串的字符转换为大写 -
import random
# input string
inputString = 'tutorialspoint'
# printing input string
print("Input String:", inputString)
# Converting string to list
stringList = list(inputString)
# Getting the number of random indexes to be modified to upper case
n = random.randint(0, len(stringList)-1)
# Getting above n number of random elements from 0 to the length of the list
randomIndexList = random.sample(range(0, len(stringList)-1), n)
# Traversing the above index list
for index in randomIndexList:
# Converting the corresponding index string element to the upper case
stringList[index] = stringList[index].upper()
# Joining all the list elements and Converting to string
result = ''.join(stringList)
# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", result)
输出
执行上述程序后,将生成以下输出 -
Input String: tutorialspoint String after converting chars lower or uppercase randomly: tUToriaLsPOINT
方法3:使用map()、choice()、zip()函数
random.choice()函数(从序列(如列表、元组、数字范围)中随机选择一个项目)。
random.choice(sequence)
zip()函数用于组合两个列表/迭代器。
算法(步骤)
以下是执行所需任务应遵循的算法/步骤 -
使用import关键字从random模块导入choice函数。
使用zip函数压缩字符串的小写和大写字符,并使用random.choice()方法从中选择一个。
使用map()函数将其应用于字符串的所有元素。
使用join()函数将此map对象转换为字符串。
随机将输入字符串的字符转换为大写后,打印结果字符串。
示例
以下程序返回一个字符串,该字符串使用map()、random.choice()和zip()函数随机将输入字符串的字符转换为大写 -
# importing choice from the random module
from random import choice
# input string
inputString = 'tutorialspoint'
print("Input String:", inputString)
# extending logic to each character using a map,
# and selecting upper or lower for each character
randchars_str = ''.join(map(choice, zip(inputString.lower(), inputString.upper())))
# printing resultant string after converting chars lower or uppercase randomly
print("String after converting chars lower or uppercase randomly:\n", randchars_str)
输出
执行上述程序后,将生成以下输出 -
Input String: tutorialspoint String after converting chars lower or uppercase randomly: TutoRIaLsPoInt
结论
在本文中,我们学习了如何使用三种不同的方法随机将字符串中的字符转换为大写。我们还学习了如何使用randInt()生成随机整数,如何使用sample()生成n个随机数作为列表,以及如何使用choice()随机选择一个项目。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP