-
Notifications
You must be signed in to change notification settings - Fork 1
/
CapitalizeTitle.py
27 lines (21 loc) · 928 Bytes
/
CapitalizeTitle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
# If the length of the word is 1 or 2 letters, change all letters to lowercase.
# Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
# Return the capitalized title.
def capitalizeTitle(title):
titleWords = title.split(" ")
newTitle = ""
for word in titleWords:
if len(word) > 2:
word = word[0].upper() + word[1:].lower()
else:
word = word.lower()
newTitle += word + " "
return newTitle.strip()
# Test cases
title = "capiTalIze tHe titLe"
print(capitalizeTitle(title))
title = "First leTTeR of EACH Word"
print(capitalizeTitle(title))
title = "i lOve leetcode"
print(capitalizeTitle(title))