반응형
SMALL
주어진 문자열에서
소문자는 대문자로 대문자는 소문자로 변환하기
변환 전 : HackerRank.com presents "Pythonist 2".
변환 후 : hACKERrANK.COM PRESENTS "pYTHONIST 2".
기본으로 주어지는 코드
def swap_case(s):
return
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
def swap_case(s):
result = "" #string은 immutable 개체이기에 s 변환 x, # result 라는 빈 string을 만들어 반환
for ch in s: # 문자열 s를 각 개체 ch로
if ch.isupper(): # ch가 대문자라면, ch.lower 소문자로 바꿔 result에저장
result+=ch.lower()
elif ch.islower(): # ch가 소문자라면, ch.upper 대문자로 바꿔 result에저장
result+=ch.upper()
else:
result+=ch
return result
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
더보기
#upper
소문자를 대문자로 변환
>>> a = "hello"
>>> a.upper()
'HELLO'
#lower
대문자를 소문자로 변환
>>> a = "WORLD"
>>> a.lower()
'wolrd'
반응형
LIST
'IT & 영상관련 > 파이썬python' 카테고리의 다른 글
python]hackerRank] Find a string (저장용) (0) | 2020.07.29 |
---|---|
python]hackerRank] String Split and Join (저장용) (1) | 2020.07.28 |
python]hackerRank] Tuples (저장용) (0) | 2020.07.26 |
python]hackerRank] lists (저장용) (0) | 2020.07.25 |
python]hackerRank] Finding the percentage, 평균값(소수점)(저장용) (0) | 2020.07.24 |