写一个 Python示例,用上面的函数,For 循环,while 循环和 ASCII 将字符串转换成大写,并举例说明。
这个 python 程序允许用户输入一个字符串。接下来,我们使用一个名为 upper 的内置字符串函数将字符串中的小写字符转换为大写字符。
提示:请参考弦文章了解蟒弦的一切。
# Python Program to Convert String to Uppercase
string = input("Please Enter your Own String : ")
string1 = string.upper()
print("\nOriginal String in Lowercase = ", string)
print("The Given String in Uppercase = ", string1)
将 Python 字符串转换为大写的输出
Please Enter your Own String : Python Example
Original String in Lowercase = Python Example
The Given String in Uppercase = PYTHON EXAMPLE
这个 python 程序允许用户输入字符串。接下来,它找到小写字母并将其转换为大写字母。
首先,我们使用 For 循环来迭代字符串中的字符。在 For 循环中,我们使用 If Else 语句来检查字符是否在 a 和 z 之间。如果为真,我们将从其 ASCII 值中减去 32。否则,我们将该字符转换为字符串 1。
提示:请参考总字符 ASCII 值一文和 ASCII 表了解 ASCII 值。
# Python Program to Convert String to Uppercase
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 Lowercase = ", string)
print("The Given String in Uppercase = ", string1)
Please Enter your Own String : python
Original String in Lowercase = python
The Given String in Uppercase = PYTHON
这个 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 Lowercase = ", string)
print("The Given String in Uppercase = ", string1)
Please Enter your Own String : Tutorial Gateway
Original String in Lowercase = Tutorial Gateway
The Given String in Uppercase = TUTORIAL GATEWAY
这个 python 字符串大写程序与第二个示例相同。然而,我们使用的是对象循环
# Python Program to Convert String to Uppercase
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 Lowercase = ", string)
print("The Given String in Uppercase = ", string1)
在这个程序中,我们比较 ASCII 值来检查这个字符串中是否有任何小写字符。如果是真的,我们会把它们转换成大写。
# Python Program to Convert String to Uppercase
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(ord(i) >= 97 and ord(i) <= 122):
string1 = string1 + chr((ord(i) - 32))
else:
string1 = string1 + i
print("\nOriginal String in Lowercase = ", string)
print("The Given String in Uppercase = ", string1)
Please Enter your Own String : Python uppercase Tutorial
Original String in Lowercase = Python uppercase Tutorial
The Given String in Uppercase = PYTHON UPPERCASE TUTORIAL
标签: claude