Update capitalize.py (#10573)

* Update capitalize.py

* Update strings/capitalize.py

---------

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
This commit is contained in:
Saurabh Mahapatra 2023-10-26 13:55:08 +05:30 committed by GitHub
parent e791a2067b
commit ade2837e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,8 @@ from string import ascii_lowercase, ascii_uppercase
def capitalize(sentence: str) -> str: 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") >>> capitalize("hello world")
'Hello world' 'Hello world'
>>> capitalize("123 hello world") >>> capitalize("123 hello world")
@ -17,6 +18,10 @@ def capitalize(sentence: str) -> str:
""" """
if not sentence: if not sentence:
return "" 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)) lower_to_upper = dict(zip(ascii_lowercase, ascii_uppercase))
return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:] return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:]