MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / capitalize

Function capitalize

strings/capitalize.py:1–21  ·  view source on GitHub ↗

Capitalizes the first letter of a sentence or word. >>> capitalize("hello world") 'Hello world' >>> capitalize("123 hello world") '123 hello world' >>> capitalize(" hello world") ' hello world' >>> capitalize("a") 'A' >>> capitalize("") ''

(sentence: str)

Source from the content-addressed store, hash-verified

1def capitalize(sentence: str) -> str:
2 """
3 Capitalizes the first letter of a sentence or word.
4
5 >>> capitalize("hello world")
6 'Hello world'
7 >>> capitalize("123 hello world")
8 '123 hello world'
9 >>> capitalize(" hello world")
10 ' hello world'
11 >>> capitalize("a")
12 'A'
13 >>> capitalize("")
14 ''
15 """
16 if not sentence:
17 return ""
18
19 # Capitalize the first character if it's a lowercase letter
20 # Concatenate the capitalized character with the rest of the string
21 return sentence[0].upper() + sentence[1:]
22
23
24if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected