2020-09-03 14:11:23 +00:00
|
|
|
from string import ascii_lowercase, ascii_uppercase
|
|
|
|
|
|
|
|
|
|
|
|
def capitalize(sentence: str) -> str:
|
|
|
|
"""
|
2023-10-26 08:25:08 +00:00
|
|
|
Capitalizes the first letter of a sentence or word.
|
|
|
|
|
2020-09-03 14:11:23 +00:00
|
|
|
>>> capitalize("hello world")
|
|
|
|
'Hello world'
|
|
|
|
>>> capitalize("123 hello world")
|
|
|
|
'123 hello world'
|
|
|
|
>>> capitalize(" hello world")
|
|
|
|
' hello world'
|
|
|
|
>>> capitalize("a")
|
|
|
|
'A'
|
|
|
|
>>> capitalize("")
|
|
|
|
''
|
|
|
|
"""
|
|
|
|
if not sentence:
|
2020-09-10 08:31:26 +00:00
|
|
|
return ""
|
2023-10-26 08:25:08 +00:00
|
|
|
|
|
|
|
# Create a dictionary that maps lowercase letters to uppercase letters
|
|
|
|
# Capitalize the first character if it's a lowercase letter
|
|
|
|
# Concatenate the capitalized character with the rest of the string
|
2023-05-26 07:34:17 +00:00
|
|
|
lower_to_upper = dict(zip(ascii_lowercase, ascii_uppercase))
|
2020-09-03 14:11:23 +00:00
|
|
|
return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|