Prepare > Python > Strings > sWAP cASE

2023. 6. 28. 21:48HackerRank-Python

https://www.hackerrank.com/challenges/swap-case/problem?isFullScreen=true

 

문제


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.

 

=> 주어진 문자를 대소문자 바꿔서 출력하라.

 

 

 

 

코드


def swap_case(s):
    fin = ""
    for i in s:
        if i.islower() == True:
            fin = fin + i.upper()
        elif i.isupper() == True:
            fin = fin + i.lower()
        else:
            fin = fin + i
    return fin

if __name__ == '__main__':

 

 

 

 

노트


  • a.isupper(): a가 대문자이면 True
  • a.islower(): a가 소문자이면 True
# INPUT
b = 'qweR'
print(b.islower())
print(b.isupper())
# OUTPUT
False
False

 

 

 

 

참조


 

[Python] 대소문자 확인 및 변환

이 글에서는 문자가 대문자인지, 소문자인지 확인하는 함수와 문자열을 대문자 또는 소문자로 변경하는 함수를 정리한다. isupper 문자가 대문자인지 확인한다. str.isupper() # return : bool # 예시 print(

passwd.tistory.com

 

 

HackerRank Python Solution / sWAP cASE

sWAP cASE def swap_case(sentence): updated_s = "" for c in sentence: if c.isupper(): updated_s += c.lower() elif c.islower(): updated_s += c.upper() else: updated_s += c return updated_s if __name__ == '__main__': s = input() result = swap_case(s) print(re

bohemihan.tistory.com