1
+ def convert_word (word : str ) -> str :
2
+ """
3
+ Converts a string to capitalized case, preserving the input as is
4
+
5
+ >>> convert_word("Aakash")
6
+ 'Aakash'
7
+
8
+ >>> convert_word("aakash")
9
+ 'Aakash'
10
+
11
+ >>> convert_word("AAKASH")
12
+ 'Aakash'
13
+
14
+ >>> convert_word("aAkAsH")
15
+ 'Aakash'
16
+ """
17
+
18
+ """
19
+ Convert the first character to uppercase if it's lowercase
20
+ """
21
+ if 'a' <= word [0 ] <= 'z' :
22
+ word = chr (ord (word [0 ]) - 32 ) + word [1 :]
23
+
24
+ """
25
+ Convert the remaining characters to lowercase if they are uppercase
26
+ """
27
+ for i in range (1 , len (word )):
28
+ if 'A' <= word [i ] <= 'Z' :
29
+ word = word [:i ] + chr (ord (word [i ]) + 32 ) + word [i + 1 :]
30
+
31
+ return word
32
+
1
33
def to_title_case (input_str : str ) -> str :
2
34
"""
3
35
Converts a string to title case, preserving the input as is
@@ -15,29 +47,12 @@ def to_title_case(input_str: str) -> str:
15
47
'Aakash Giri'
16
48
"""
17
49
18
- def convert_word (word ):
19
- """
20
- Convert the first character to uppercase if it's lowercase
21
- """
22
- if "a" <= word [0 ] <= "z" :
23
- word = chr (ord (word [0 ]) - 32 ) + word [1 :]
24
-
25
- """
26
- Convert the remaining characters to lowercase if they are uppercase
27
- """
28
- for i in range (1 , len (word )):
29
- if "A" <= word [i ] <= "Z" :
30
- word = word [:i ] + chr (ord (word [i ]) + 32 ) + word [i + 1 :]
31
-
32
- return word
33
-
34
50
words = input_str .split ()
35
51
title_case_str = [convert_word (word ) for word in words ]
36
52
37
- return " " .join (title_case_str )
38
-
53
+ return ' ' .join (title_case_str )
39
54
40
55
if __name__ == "__main__" :
41
56
from doctest import testmod
42
57
43
- testmod ()
58
+ testmod ()
0 commit comments