编写一个 Python示例,使用下面的函数将字符串转换为小写,例如 For 循环、while 循环和 ASCII。
这个 python 程序允许用户输入一个字符串。接下来,我们使用一个名为 lower 的内置字符串函数将字符串中的大写字符转换为小写字符。
提示:请参考弦文章了解蟒弦的一切。
# Python Program to Convert String to Lowercase
string = input("Please Enter your Own String : ")
string1 = string.lower()
print("\nOriginal String in Uppercase = ", string)
print("The Given String in Lowercase = ", string1)
将 Python 字符串转换为小写输出
Please Enter your Own String : HELLO WORLD
Original String in Uppercase = HELLO WORLD
The Given String in Lowercase = hello world
这个 python 程序允许用户输入字符串。接下来,它找到大写字母并将它们转换成小写字母。
首先,我们使用 For 循环来迭代字符串中的字符。在 For 循环中,我们使用 If Else 语句来检查字符是否在 A 和 Z 之间。如果为真,我们将在它的 ASCII 值上增加 32。否则,我们将该字符转换为字符串 1。
提示:请参考总字符 ASCII 值一文和 ASCII 表了解 ASCII 值。
# Python Program to Convert String to Lowercase
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))
else:
string1 = string1 + string[i]
print("\nOriginal String in Uppercase = ", string)
print("The Given String in Lowercase = ", string1)
Please Enter your Own String : WELCOME TO TUtoriaL GATEWAY
Original String in Uppercase = WELCOME TO TUtoriaL GATEWAY
The Given String in Lowercase = welcome to tutorial gateway
这个 python 字符串大写到小写的转换程序同上。然而,我们只是将 For 循环替换为 While 循环。
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))
else:
string1 = string1 + string[i]
i = i + 1
print("\nOriginal String in Uppercase = ", string)
print("The Given String in Lowercase = ", string1)
Please Enter your Own String : PYTHON TUTORIAL
Original String in Uppercase = PYTHON TUTORIAL
The Given String in Lowercase = python tutorial
这个将大写改为小写的 Python 代码与第二个示例相同。然而,我们使用的是对象循环
# Python Program to Convert String to Lowercase
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(i >= 'A' and i <= 'Z'):
string1 = string1 + chr((ord(i) + 32))
else:
string1 = string1 + i
print("\nOriginal String in Uppercase = ", string)
print("The Given String in Lowercase = ", string1)
在这个程序中,我们比较 ASCII 值来检查是否有大写字符。如果是真的,我们将它们转换成小写。
# Python Program to Convert String to Lowercase
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))
else:
string1 = string1 + i
print("\nOriginal String in Uppercase = ", string)
print("The Given String in Lowercase = ", string1)
Please Enter your Own String : TUTORIAL GATEWAY
Original String in Uppercase = TUTORIAL GATEWAY
The Given String in Lowercase = tutorial gateway
标签: sublime