문제 설명
문자열 my_string이 매개변수로 주어질 때, 대문자는 소문자로 소문자는 대문자로 변환한 문자열을 return하도록 solution 함수를 완성해주세요.
swapcase()
- The swapcase() method returns a string where all the upper case letters are lower case and vice versa.
대문자 ↔ 소문자 - string.swapcase()
- 참고자료: W3schools - Python String swapcase() Method
def solution(my_string):
answer = ''
for i in my_string:
if i.isupper():
answer += i.lower()
else:
answer += i.upper()
return answer
- for문 → if문
- my_string을 한 글자씩 i로 for문을 돌립니다.
- i.isupper(): i가 대문자라면 i.lower() 소문자로 바꿔줍니다.
- 대문자가 아니라면 즉, 소문자라면 i.upper() 대문자로 바꿔줍니다.
Code - 2차
def solution(my_string):
return my_string.swapcase()
- swapcase()
- swapcase: 위에서 설명한 것처럼 my_string 값을 하나씩 비교해 대문자는 소문자로, 소문자는 대문자로 바꿀 수 있는 함수입니다. 본 함수를 이용하면 for문과 if문을 사용하지 않고도 문제를 풀 수 있습니다. 아는 것이 힘이다!
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] A 강조하기 - 181874, lower(), replace() (0) | 2024.08.17 |
---|---|
[Python/level 0] 길이에 따른 연산 - 181879, prod() (0) | 2024.08.17 |
[Python/level 0] 특정한 문자를 대문자로 바꾸기 - 181873, replace() (0) | 2024.08.17 |
[Python/level 0] 문자 반복 출력하기 - 120825, join() (1) | 2024.08.17 |
[Python/level 0] 특정 문자 제거하기 - 120826, replace() (0) | 2024.08.17 |