hackerrank String 1 swap cases

You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.

For Example:

Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2

Input Format

A single line containing a string .

Constraints

 

Output Format

Print the modified string .

Sample Input 0

HackerRank.com presents "Pythonist 2".

Sample Output 0

hACKERrANK.COM PRESENTS "pYTHONIST 2".

1. 使用upper()  and lower() 方法

def swap_case(s):
    lists = list(s)
    for i in range(0,len(lists)):
        c = lists[i]

        if ord(c)>95 and ord(c)<123:
            lists[i] = c.upper()
        elif ord(c)>63 and ord(c)<91:
            lists[i] = c.lower()

    result  = ''.join(lists)
    return result

if __name__ == '__main__':
    s = raw_input()
    result = swap_case(s)
    print result

2. 使用swapcase()

def swap_case(s):

    return s.swapcase()

if __name__ == '__main__':
    s = raw_input()
    result = swap_case(s)
    print result

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值