File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed
Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change 1- from string import ascii_lowercase , ascii_uppercase
2-
3-
41def capitalize (sentence : str ) -> str :
52 """
6- Capitalizes the first letter of a sentence or word .
3+ Capitalizes the first character of the string if it is a lowercase letter .
74
85 >>> capitalize("hello world")
96 'Hello world'
@@ -19,11 +16,14 @@ def capitalize(sentence: str) -> str:
1916 if not sentence :
2017 return ""
2118
22- # Create a dictionary that maps lowercase letters to uppercase letters
23- # Capitalize the first character if it's a lowercase letter
24- # Concatenate the capitalized character with the rest of the string
25- lower_to_upper = dict (zip (ascii_lowercase , ascii_uppercase ))
26- return lower_to_upper .get (sentence [0 ], sentence [0 ]) + sentence [1 :]
19+ # Get the first character of the sentence
20+ first_char = sentence [0 ]
21+ # Check if the first character is a lowercase letter
22+ if 'a' <= first_char <= 'z' :
23+ # Convert the lowercase letter to uppercase using ASCII value
24+ first_char = chr (ord (first_char ) - 32 )
25+ # Return the capitalized first character concatenated with the rest of the sentence
26+ return first_char + sentence [1 :]
2727
2828
2929if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments