[Python] Programmers - Creating a JadenCase string
I have summarized the solution for creating a JadenCase string while preserving consecutive spaces using split(" ") and join() in Python.
한국어 원문은 여기에서 볼 수 있습니다.
[Python] Programmers - Creating a JadenCase string
Solution
1
2
3
4
5
6
7
8
9
def solution(s):
answer = []
for i in s.split(" "):
answer.append(i[:1].upper() + i[1:].lower())
return " ".join(answer)
- Whether the first letter of a word in a sentence is an alphabet or a number, the second letter of the word is lowercase!
- At first, the code was long by checking whether the first letter was an alphabet through
.isalpha(), or applying ~ if it was not an alphabet, but it was shortened.
- At first, the code was long by checking whether the first letter was an alphabet through
- Trap with continuous blank spaces
- Test cases keep failing because of this trap
s.split(" "): Leave consecutive spaces as empty string “”- If there are two spaces attached, the two spaces disappear through the code above and are treated as one.
" ".join(answer)
- Code that concatenates the strings in the list with a single space “ “
- If you use join(), there will be no unnecessary spaces at the end and it will be much neater.
- What if the word is the last word to remove unnecessary spaces at the end? ~ I added the same code, but it is no longer needed.