From ade2837e410ec286819f0f4fd977bb411a95b379 Mon Sep 17 00:00:00 2001 From: Saurabh Mahapatra <98408932+its-100rabh@users.noreply.github.com> Date: Thu, 26 Oct 2023 13:55:08 +0530 Subject: [PATCH] Update capitalize.py (#10573) * Update capitalize.py * Update strings/capitalize.py --------- Co-authored-by: Tianyi Zheng --- strings/capitalize.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index e7e97c2be..c0b45e0d9 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -3,7 +3,8 @@ from string import ascii_lowercase, ascii_uppercase 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") @@ -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:]