Python程序:将字符串的字符转换为相反的大小写

Python程序:将字符串的字符转换为相反的大小写

在这个问题中,我们将切换每个字符串字符的大小写。切换每个字符串字符大小写的最简单方法是使用swapcase()内置方法。

此外,我们还可以使用字符的ASCII值来交换它们的大小写。Python还包含isUpper()和isLower()方法来检查字符的大小写以及lower()和upper()方法来改变大小写。

在这里,我们将学习解决问题的不同方法。

问题陈述 - 我们给出了一个字符串alpha。我们需要切换字符串字符的大小写。这意味着将大写字符转换为小写,将小写字符转换为大写。

示例示例

输入

alpha = "TutorialsPoint"

输出

'tUTORIALSpOINT'

解释 – 在这里,我们转换了每个字符的大小写。

输入

alpha = “AAAA”

输出

'aaaa'

输入

alpha = '12qwER@'

输出

'12QWer@'

解释 – 我们改变了每个字母字符的大小写,并保留特殊字符不变。

方法1

在这种方法中,如果字符是大写字母,则在ASCII值上加32,否则在ASCII值上减去32以将字符转换为大写。

算法

步骤1 - 将字符串转换为字符列表。

步骤2 - 遍历字符列表。

步骤3 - 如果字符在’A’到’Z’之间,则在ASCII值上加32,并更新字符列表中的字符。

步骤4 - 如果字符在’a’到’z’之间,则从字符的ASCII值中减去32,并更新字符列表中的字符。

步骤5 - 使用join()方法将列表的所有字符转换为字符串。

步骤6 - 返回结果字符串。

示例

def changeCase(alpha):
   alpha_list = list(alpha)  # String to list
# Traverse list of string characters
   for p in range(len(alpha_list)):
     if alpha_list[p] >= 'A' and alpha_list[p] <= 'Z':
       alpha_list[p] = chr(ord(alpha_list[p]) + 32)  # Change to lowercase
     elif alpha_list[p] >= 'a' and alpha_list[p] <= 'z':
       alpha_list[p] = chr(ord(alpha_list[p]) - 32)  # Change to uppercase

   alpha = ''.join(alpha_list)  # List to string
   return alpha
alpha = "TutorialsPoint"
alpha = changeCase(alpha)
print("The string after flipping the case of each character is -", alpha)

输出

The string after flipping the case of each character is - tUTORIALSpOINT

时间复杂度 – 使用O(N)遍历字符串的字符。

空间复杂度 – 使用O(N)将字符串存储在字符列表中。

方法2

在这种方法中,我们使用isUpper()方法来检查字符是否为大写。此外,我们使用upper()和lower()方法分别将字符转换为大写和小写。

算法

步骤1 - 将 ‘temp’ 初始化为空字符串,用于存储结果字符串。

步骤2 - 迭代字符串。

步骤3 - 如果一个字符是大写的,使用lower()方法将字符转换为小写并将其附加到temp字符串。

步骤4 - 否则,使用upper()方法将字符转换为大写并将其附加到temp字符串。

步骤5 - 返回 ‘temp’ 字符串。

示例

def changeCase(alpha):
   temp = ""
   for ch in alpha:
     # Using the isupper() method to check if the character is uppercase or not
     if (ch.isupper()):
       # Converting a character to lowercase
       temp += ch.lower()
     else:
       temp += ch.upper()  # Converting a character to uppercase
   return temp
alpha = "TutorialsPoint"
alpha = changeCase(alpha)
print("The string after flipping the case of each character is -", alpha)

输出

The string after flipping the case of each character is - tUTORIALSpOINT

时间复杂度 – O(N)用于遍历字符串。

空间复杂度 – O(N)用于存储结果字符串。

方法3

在这个方法中,我们使用swapcase()方法来切换字符的大小写。在切换每个字符的大小写后,我们将alpha字符串存储到自身。

示例

def changeCase(alpha):
   # Using the swapcase() function to toggle the case of characters of the string
   alpha = alpha.swapcase()
   return alpha
alpha = "AbcSDe"
alpha = changeCase(alpha)
print("The string after flipping the case of each character is -", alpha)

输出

The string after flipping the case of each character is - aBCsdE

时间复杂度 – O(N)

空间复杂度 – O(1)

方法4

在这个方法中,我们将使用正则表达式和lambda函数来切换每个字符串字符的大小写。

算法

步骤1 - 将’patt’初始化为搜索小写和大写字母字符。

步骤2 - 然后,使用re.sub()方法将所有字母字符替换为它们的相反大小写。

步骤3 - 将’patt’作为sub()方法的第一个参数,lambda函数作为第二个参数来切换字符,将原始字符串作为第三个参数。

步骤4 - 返回alpha字符串。

示例

import re
def changeCase(alpha):
   # Regular expression
   patt = r'[a-zA-Z]'

   # Toggle case of each character
   alpha = re.sub(patt, lambda ch: ch.group().lower()
         if ch.group().isupper() else ch.group().upper(), alpha)
   return alpha
alpha = "TutorialsPoint"
alpha = changeCase(alpha)
print("The string after flipping the case of each character is -", alpha)

输出

The string after flipping the case of each character is - tUTORIALSpOINT

时间复杂度 – O(N)

空间复杂度 – O(1)

方法5

在这个方法中,我们将字符的ASCII值与32进行异或操作以切换大小写。

当我们对任何字母字符与32进行异或时,它会切换ASCII值的二进制表示的第6位。

让我们通过下面的例子来理解。

  • 65 = ‘A’ = 1000001

  • 97 = ‘a’ = 1100001

  • 32 = 0100000

因此,当我们对65和32进行异或时,它变为97,因为第6位被翻转了,同样,97和32的异或结果是32。

这适用于所有字母字符的ASCII值。

算法

步骤1 - 开始遍历字符串。

步骤2 - 执行isAlphabeticChar()函数,检查当前字符是否是字母字符。

步骤3 - 在isAlphabeticChar()函数中,如果ASCII值在65到90或97到122之间,则返回true。否则,返回false。

步骤4 - 如果当前字符是字母字符,将其与32进行异或,并将更新后的字符追加到’temp’字符串。

步骤5 - 否则,将原始字符追加到temp字符串中。

步骤6 - 返回temp字符串。

例子

def isAlphbeticChar(char):
    Ascii = ord(char[0])

    # Using ASCII values to validate character
    if((Ascii >= 65 and Ascii <= 90) or (Ascii >= 97 and Ascii <= 122)):
        return True
    else:
        return False

def changeCase(alpha):
    temp = ""
    for ch in range(len(alpha)):
        if(isAlphbeticChar(alpha[ch])):
            temp += chr(ord(alpha[ch]) ^ (1 << 5))
        else:
            temp += alpha[ch]
    return temp
alpha = "TutorialsPoint"
alpha = changeCase(alpha)
print("The string after flipping the case of each character is -", alpha)

输出

The string after flipping the case of each character is - tUTORIALSpOINT

时间复杂度 – O(N)

空间复杂度 – O(N)

我们学习了5种方法来切换字符串的每个字符。第一种方法使用ASCII值,第二种方法使用isUpper()、upper()和lower()方法。

此外,第三种方法使用swapcase()方法,第四种方法使用正则表达式,最后一种方法使用XOR运算符。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程