Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion strings/capitalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

def capitalize(sentence: str) -> str:
"""
This function will capitalize the first letter of a sentence or a word
Capitalizes the first letter of a sentence or word.

>>> capitalize("hello world")
'Hello world'
>>> capitalize("123 hello world")
Expand All @@ -17,6 +18,10 @@ def capitalize(sentence: str) -> str:
"""
if not sentence:
return ""

# 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
lower_to_upper = dict(zip(ascii_lowercase, ascii_uppercase))
return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:]

Expand Down