MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / to_title_case

Function to_title_case

strings/title.py:1–31  ·  view source on GitHub ↗

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'

(word: str)

Source from the content-addressed store, hash-verified

1def to_title_case(word: str) -> str:
2 """
3 Converts a string to capitalized case, preserving the input as is
4
5 >>> to_title_case("Aakash")
6 'Aakash'
7
8 >>> to_title_case("aakash")
9 'Aakash'
10
11 >>> to_title_case("AAKASH")
12 'Aakash'
13
14 >>> to_title_case("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
33
34def sentence_to_title_case(input_str: str) -> str:

Callers 1

sentence_to_title_caseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected