python 字符串大小写转换(不能使用swapcase()方法)

python 3字符串大小写转换

要求不能使用swapcase()方法

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan

str1 = input("请输入字符串:")

list1 = list(str1)
str2 = ''

for i in list1:
    if int(ord(i)) >= 65 and int(ord(i)) <= 90:   #大写
        str2 += chr(int(ord(i))+32)           #字符串拼接
    elif int(ord(i)) >= 97 and int(ord(i)) <= 122:  #小写
        str2 += chr(int(ord(i)) - 32)

print(str2)

  

原文地址:https://www.cnblogs.com/hiuhungwan/p/9210881.html