Skip to content

Update capitalize.py #10573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2023
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