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()隨機選擇一個專案。

更新於: 2023年1月27日

1K+ 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.