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
-
4
1
def capitalize (sentence : str ) -> str :
5
2
"""
6
- Capitalizes the first letter of a sentence or word .
3
+ Capitalizes the first character of the string if it is a lowercase letter .
7
4
8
5
>>> capitalize("hello world")
9
6
'Hello world'
@@ -19,11 +16,14 @@ def capitalize(sentence: str) -> str:
19
16
if not sentence :
20
17
return ""
21
18
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 :]
27
27
28
28
29
29
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments