forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.py
57 lines (40 loc) · 1.28 KB
/
title.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
defto_title_case(word: str) ->str:
"""
Converts a string to capitalized case, preserving the input as is
>>> to_title_case("Aakash")
'Aakash'
>>> to_title_case("aakash")
'Aakash'
>>> to_title_case("AAKASH")
'Aakash'
>>> to_title_case("aAkAsH")
'Aakash'
"""
"""
Convert the first character to uppercase if it's lowercase
"""
if"a"<=word[0] <="z":
word=chr(ord(word[0]) -32) +word[1:]
"""
Convert the remaining characters to lowercase if they are uppercase
"""
foriinrange(1, len(word)):
if"A"<=word[i] <="Z":
word=word[:i] +chr(ord(word[i]) +32) +word[i+1 :]
returnword
defsentence_to_title_case(input_str: str) ->str:
"""
Converts a string to title case, preserving the input as is
>>> sentence_to_title_case("Aakash Giri")
'Aakash Giri'
>>> sentence_to_title_case("aakash giri")
'Aakash Giri'
>>> sentence_to_title_case("AAKASH GIRI")
'Aakash Giri'
>>> sentence_to_title_case("aAkAsH gIrI")
'Aakash Giri'
"""
return" ".join(to_title_case(word) forwordininput_str.split())
if__name__=="__main__":
fromdoctestimporttestmod
testmod()