[프로그래머스] JadenCase 문자열 만들기 (Python)

[프로그래머스] JadenCase 문자열 만들기

풀이

1
2
3
4
5
6
7
8
def solution(s):
    answer = ''
    s = s.split(' ')

    for i in range(len(s)):
        answer += s[i].capitalize() + " "

    return answer[:-1]

처음에는 islower()/isupper()를 통해 대문자 및 소문자를 구분한 다음 lower()/upper()를 이용하여 JadenCase 문자열로 바꾸었는데, capitalize() 함수 사용을 통해 코드를 훨씬 간결하게 작성할 수 있었다.



📌 capitalize() : 문자열의 첫 글자만 대문자로 바꿈 (나머지는 소문자)

string = "this is an Apple"
print(string.capitalize())

# This is an apple

📌 split()와 split(“ “)의 결과는 다름

string = "a b  c   d"
print(string.split())
# ['a', 'b', 'c', 'd']

print(string.split(" "))
# ['a', 'b', '', 'c', '', '', 'd']