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