Skip to content

Simplify Capitalize Function #12879

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
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: 1 addition & 6 deletions strings/capitalize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from string import ascii_lowercase, ascii_uppercase


def capitalize(sentence: str) -> str:
"""
Capitalizes the first letter of a sentence or word.
Expand All @@ -19,11 +16,9 @@ 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:]
return sentence[0].upper() + sentence[1:]


if __name__ == "__main__":
Expand Down