Posts sWAP cASE (Python 3)
Post
Cancel

sWAP cASE (Python 3)

  • URL : https://www.hackerrank.com/challenges/swap-case/problem

  • 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:
    1
    2
    
      Www.HackerRank.com  wWW.hACKERrANK.COM
      Pythonist 2  pYTHONIST 2
    
  • Input Format
    • A single line containing a string S.
  • Constraints
    • 0 <= len(s) <= 1000
  • Output Format
    • Print the modified string S.

문제 풀이

  • string s가 주어졌을떄, 소문자는 대문자로, 대문자는 소문자로 바꾸는 함수를 작성
  • s를 for문으로 돌면서 대문자이면 소문자로, 소문자면 대문자로 둘다 아니라면 그냥 그대로 두는 코드를 만들었다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def swap_case(s):
    s_ = ''
    for i in s:
        if i.isupper():
            i = i.lower()
            s_ += i
        elif i.islower():
            i = i.upper()
            s_ += i
        else:
            s_ += i
    return s_

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)
1
2
3
4
 HackerRank.com presents "Pythonist 2".


hACKERrANK.COM PRESENTS "pYTHONIST 2".
1
This post is licensed under CC BY 4.0 by the author.