forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapitalize.py
More file actions
Latest commit
27 lines (22 loc) · 679 Bytes
/
Copy pathcapitalize.py
File metadata and controls
27 lines (22 loc) · 679 Bytes
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
fromstringimportascii_lowercase, ascii_uppercase
defcapitalize(sentence: str) ->str:
"""
This function will capitalize the first letter of a sentence or a word
>>> capitalize("hello world")
'Hello world'
>>> capitalize("123 hello world")
'123 hello world'
>>> capitalize(" hello world")
' hello world'
>>> capitalize("a")
'A'
>>> capitalize("")
''
"""
ifnotsentence:
return""
lower_to_upper= {lc: ucforlc, ucinzip(ascii_lowercase, ascii_uppercase)}
returnlower_to_upper.get(sentence[0], sentence[0]) +sentence[1:]
if__name__=="__main__":
fromdoctestimporttestmod
testmod()