如何將Python字串轉換為交替大小寫?


在本文中,使用者將學習如何將Python字串轉換為交替大小寫。字串由字元組成,並用單引號或雙引號括起來。可以使用Python語言將字串轉換為交替大小寫。在給定的字串中,可以將字串從大寫轉換為小寫,反之亦然。本文演示了三種方法。第一種方法描述了for迴圈的概念,第二種方法使用了join()方法,第三種方法使用了正則表示式將Python字串轉換為交替大小寫。

方法

方法 1 - 利用for迴圈

方法 2 - 利用join()方法

方法 3 - 利用正則表示式

方法 1:使用for迴圈將Python字串轉換為交替大小寫的Python程式

for迴圈用於遍歷字串的元素,並根據字串的範圍和長度返回小寫或大寫字元。

演算法

  • 步驟 1 - 使用字串值初始化字串。

  • 步驟 2 - 然後宣告空字串。

  • 步驟 3 - 使用for迴圈遍歷字串的長度。

  • 步驟 4 - 當字元的索引為偶數時,它將字串返回為小寫。

  • 步驟 5 - 當字元的索引為奇數時,它將字串返回為大寫。

  • 步驟 6 - 然後列印語句將返回轉換後的交替大小寫。

示例

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized
new_str = ""
#for loop is used to iterate through the loop along with the range of the length of list
for a in range(len(str_1)):
   if a % 2 == 0:
      #it returns the lowercase of the string when it is even
      new_str += str_1[a].lower()
   else:
      #it returns the uppercase of the string when it is odd
      new_str += str_1[a].upper()
#return the string after converting it into another case
print("The string after alternating case is:",new_str)

輸出

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

方法 2:使用join()方法將Python字串轉換為交替大小寫的Python程式

join()方法在空字串內宣告,並根據字串的長度返回小寫或大寫字元。

演算法

  • 步驟 1 - 步驟 1:使用字串值初始化字串。

  • 步驟 2 - 然後使用join()方法宣告空字串。

  • 步驟 3 - 使用for迴圈遍歷字串的長度。

  • 步驟 4 - 當字元的索引為偶數時,它將字串返回為小寫。

  • 步驟 5 - 當字元的索引為奇數時,它將字串返回為大寫。

  • 步驟 6 - 然後列印語句將返回轉換後的交替大小寫。

示例

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized with an join() function 
#it returns the lowercase of the string when it is even
new_str = ''.join([str_1[a].lower() 
if a % 2 == 0 
else 
#it returns the uppercase of the string when it is odd
str_1[a].upper() 
#for loop is used to iterate through the loop along with the range of the length of list
for a in range(len(str_1))])
#return the string after converting it into another case
print("The string after alternating case is:", new_str)

輸出

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

方法 3:使用正則表示式將Python字串轉換為交替大小寫的Python程式

re.sub()函式在空字串內宣告,並根據字串的長度返回小寫或大寫字元。

演算法

  • 步驟 1 - 匯入re模組以使用re.sub()函式。

  • 步驟 2 - 使用字串值初始化字串。

  • 步驟 3 - 然後使用lambda方法宣告空字串。

  • 步驟 4 - key引數用於將每個字串轉換為小寫或大寫。

  • 步驟 5 - 當字元的索引為偶數時,它將字串返回為小寫。

  • 步驟 6 - 當字元的索引為奇數時,它將字串返回為大寫。

  • 步驟 7 - 然後列印語句將返回轉換後的交替大小寫。

示例

#importing the re module to use the lambda function
import re

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized with a regular expression using the key parameter 
#it returns the lowercase of the string when it is even
new_str = re.sub(r"(.)", lambda a: a.group(1).lower() 
if a.start() % 2 == 0 
else 
#it returns the uppercase of the string when it is odd
a.group(1).upper(), str_1)
#return the string after converting it into another case
print("The string after alternating case is:", new_str)

輸出

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

結論

Python 由於其簡單性和與其他應用程式的靈活性的特點而風靡全球。與其他語言相比,它因其優勢而佔據了獨特的地位,例如易於使用簡單的語法和語句進行編碼。它正在谷歌、NASA 和 YouTube 等頂級公司中得到實施。

更新於: 2023年8月25日

862 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.