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

 

發佈了27 篇原創文章 · 獲贊 1 · 訪問量 5468
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章