首页 > 编程语言 > 详细

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

时间:2018-06-21 21:50:44      阅读:218      评论:0      收藏:0      [点我收藏+]

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)

  

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!