-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathcapitalize.py
More file actions
27 lines (23 loc) · 699 Bytes
/
capitalize.py
File metadata and controls
27 lines (23 loc) · 699 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
def capitalize(sentence: str) -> str:
"""
Capitalizes the first alphabetical character of a sentence.
Examples:
>>> capitalize("hello world")
'Hello world'
>>> capitalize(" hello world")
' Hello world'
>>> capitalize("123 abc")
'123 abc'
>>> capitalize("ñandú bird")
'Ñandú bird'
>>> capitalize("")
''
"""
if not sentence:
return ""
# Find the first alphabetic character and capitalize it
for idx, char in enumerate(sentence):
if char.isalpha():
return sentence[:idx] + char.upper() + sentence[idx + 1 :]
# If no alphabetic character exists, return the original string
return sentence