Python 程式實現選擇性索引大寫


在本文中,我們將學習一個 Python 程式,用於將給定選擇性索引的字串元素轉換為大寫。

使用的方法

以下是用以完成此任務的各種方法 -

  • 使用 For 迴圈和 upper() 函式

  • 使用列表推導式

  • 使用 index() 和 join() 函式

示例

假設我們已獲取一個輸入字串和一個包含索引值的列表。我們現在將把輸入字串中這些給定索引處的字元轉換為大寫,並列印結果字串。

輸入

inputString = 'hello tutorialspoint python'
indexList = [0, 6, 8, 10, 15, 21, 22]

輸出

Hello TuToRialsPoint PYthon

在此示例中,輸入字串中輸入列表索引0、6、8、10、15、21、22處的字元被大寫。

方法 1:使用 For 迴圈和 upper() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟 -。

  • 建立一個變數來儲存輸入字串並列印給定的輸入字串。

  • 建立另一個變數來儲存輸入索引列表。

  • 初始化一個空字串以儲存結果字串。

  • 使用for 迴圈遍歷輸入字串的每個字元,直到其長度,使用len()函式(返回物件中的專案數)。

  • 使用if 條件語句檢查當前索引是否在輸入索引列表中,使用“in”運算子

  • 使用upper()函式(將所有字元轉換為大寫)將輸入字串的相應字元轉換為大寫。

  • 如果條件為true,則使用 + 運算子(字串連線)將大寫字元新增到結果字串中。

  • 否則,直接將當前字元新增到結果字串中,無需修改。

  • 在將輸入字串的字元在輸入索引處轉換為大寫後,列印結果字串。

示例

以下程式使用 for 迴圈和 upper() 函式在給定的輸入索引處將輸入字串字元轉換為大寫後返回一個字串 -

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# initializing empty string for storing resultant string
resultantStr = ''

# travsering till the length of an input string
for i in range(0, len(inputString)):
   
   # checking whether the current index is in the input indices list
   if i in indexList:
      
      # converting that corresponding character of the input string into uppercase
      
      # and adding to the resultant string
      resultantStr += inputString[i].upper()
   else:
      
      # else adding that char to the resultant string directly without modifying
      resultantStr += inputString[i]

# printing resultant string
print("Resultant string after converting chars to uppercase at the input indices:\n", resultantStr)

輸出

執行上述程式後,將生成以下輸出 -

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at the input indices:
Hello TuToRialsPoint PYthon

方法 2:使用列表推導式

列表推導式

當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

join() - join() 是 Python 中的一個字串函式,用於連線由字串分隔符分隔的序列元素。

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用列表推導式遍歷字串的長度,並檢查此索引是否存在於索引列表中,如果存在,則轉換為大寫,否則新增相同的字元。

  • 使用 join() 函式將結果列表轉換為字串,並將分隔符作為空字串傳遞。

示例

以下程式使用列表推導式在給定的輸入索引處將輸入字串字元轉換為大寫後返回一個字串 -

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# Using list comprehension to achieve the same in the oneliner
resultantList = [inputString[i].upper() if i in indexList else inputString[i] for i in range(0, len(inputString))]

# Converting the result list to a string using the join() function.
resultantStr = ''.join(resultantList)

# printing resultant string
print("Resultant string after converting chars to uppercase at input indices:\n", resultantStr)

輸出

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at input indices:
Hello TuToRialsPoint PYthon

方法 3:使用 index() 和 join() 函式

index() 函式

index() 函式返回提供的值在第一次出現時的位置。

語法

list.index(element)

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 建立一個變數來儲存所有小寫字母作為字串。

  • 建立一個變數來儲存所有大寫字母作為字串。

  • 使用list()函式將輸入字串轉換為列表(字元列表)。

  • 使用 for 迴圈遍歷輸入索引列表的每個索引。

  • 使用 index() 函式獲取當前字元在小寫字母字串中的索引,並從大寫字母字串中獲取相應索引的大寫字元。

  • 再次使用join()函式將上述結果列表轉換為字串。

示例

以下程式使用 index() 和 join() 函式在給定的輸入索引處將輸入字串字元轉換為大寫後返回一個字串 -

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# initializing all the lowercase alphabets
lowercase_alpha = "abcdefghijklmnopqrstuvwxyz"

# initializing all the uppercase alphabets
uppercase_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# converting input string into a list (list of chars)
inputString = list(inputString)

# traversing through each index of input indices list
for i in indexList:
   
   # Converting index to upper case
   inputString[i] = uppercase_alpha[lowercase_alpha.index(inputString[i])]

# Joining the list to convert it to a string
inputString = "".join(inputString)

# printing resultant string
print("Resultant string after converting chars to uppercase at the input indices:\n", inputString)

輸出

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at the input indices:
Hello TuToRialsPoint PYthon

結論

對於給定的選擇性索引,我們在本文中學習了三種不同的方法來將字串元素更改為大寫。此外,我們學習瞭如何使用 index() 函式查詢可迭代物件(包括列表、元組、集合等)中任何元素或專案的索引。

更新於: 2023年1月27日

1K+ 閱讀量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告