Python
The capitalize function should receive a word as a parameter in
the form of a string and return one
version where the first character is written in capital letters.
Other characters must be unchanged. For example
‘hey’ become ‘Hey’. Write the function clearly. Consider that
str has the method upper that returns
a new string where all characters are capitalized
capitalize function:
def capitalize(str):
return str.upper()[0]+str[1:]
str.upper() returns the string with all the uppercase characters. using [0] we fetch the first character of that uppercased string. In this character, we add the rest of the characters of str starting from index 1 to end. This is done using str[1:].
To test this function, we can write the following lines:
result = capitalize("hey")
print(result)
Sample Output:
Refer the following screenshot for indentation and overall idea:
Get Answers For Free
Most questions answered within 1 hours.