编写一个 Python示例,使用交换大小写、For 循环、while 循环和 ASCII 在字符串中切换字符大小写。
这个 python 程序允许用户输入一个字符串。接下来,我们使用 swapcace 字符串函数将字符串中的小写字符转换为大写,大写字符转换为小写。
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = string.swapcase()
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python 切换字符串大小写输出
Please Enter your Own String : Python PROgrams
Original String = Python PROgrams
The Given String After Toggling Case = pYTHON proGRAMS
这个 python 程序允许用户输入字符串。接下来,它将小写字母切换为大写,将大写字符切换为小写。
首先,我们使用 For Loop 来迭代一个字符串中的字符。在 Python For Loop 中,我们使用的是 Elif 语句。
提示:请参考总字符 ASCII 值一文和 ASCII 表了解 ASCII 值。接下来参考转换成大写、转换成小写的文章了解逻辑。
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
for i in range(len(string)):
if(string[i] >= 'a' and string[i] <= 'z'):
string1 = string1 + chr((ord(string[i]) - 32))
elif(string[i] >= 'A' and string[i] <= 'Z'):
string1 = string1 + chr((ord(string[i]) + 32))
else:
string1 = string1 + string[i]
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python 切换字符串大小写输出
Please Enter your Own String : HellO WOrlD
Original String = HellO WOrlD
The Given String After Toggling Case = hELLo woRLd
这个 python 切换用例程序与上面的相同。然而,我们只是将 For 循环替换为 While 循环。
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
i = 0
while(i < len(string)):
if(string[i] >= 'a' and string[i] <= 'z'):
string1 = string1 + chr((ord(string[i]) - 32))
elif(string[i] >= 'A' and string[i] <= 'Z'):
string1 = string1 + chr((ord(string[i]) + 32))
else:
string1 = string1 + string[i]
i = i + 1
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python 切换字符串大小写输出
Please Enter your Own String : TuTORiaL GAteWaY
Original String = TuTORiaL GAteWaY
The Given String After Toggling Case = tUtorIAl gaTEwAy
这个 python 字符串转换程序与第二个示例相同。然而,我们使用的是对象循环。
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(i >= 'a' and i <= 'z'):
string1 = string1 + chr((ord(i) - 32))
elif(i >= 'A' and i <= 'Z'):
string1 = string1 + chr((ord(i) + 32))
else:
string1 = string1 + i
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
在这个程序中,我们将每个字符与 ASCII 值进行比较,找出这个字符串中的小写和大写字符。如果是真的,我们正在切换他们的案件。
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(ord(i) >= 65 and ord(i) <= 90):
string1 = string1 + chr((ord(i) + 32))
elif(ord(i) >= 97 and ord(i) <= 122):
string1 = string1 + chr((ord(i) - 32))
else:
string1 = string1 + i
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python 切换字符串大小写输出
Please Enter your Own String : ToGGLe StRINg CasEs
Original String = ToGGLe StRINg CasEs
The Given String After Toggling Case = tOgglE sTrinG cASeS
标签: 心情