Introduction
In Python, we can capitalize the first letter of a string using various methods. In this challenge, you are required to write a function that capitalizes the first letter of a given string.
This tutorial is from open-source community. Access the source code
In Python, we can capitalize the first letter of a string using various methods. In this challenge, you are required to write a function that capitalizes the first letter of a given string.
Write a Python function called capitalize_string(s, lower_rest=False)
that takes a string as an argument and returns a new string with the first letter capitalized. The function should have an optional parameter lower_rest
which, if set to True
, converts the rest of the string to lowercase.
def capitalize(s, lower_rest = False):
return ''.join([s[:1].upper(), (s[1:].lower() if lower_rest else s[1:])])
capitalize('fooBar') ## 'FooBar'
capitalize('fooBar', True) ## 'Foobar'
In this challenge, you have learned how to capitalize the first letter of a string in Python. You can use the capitalize()
method or write a custom function to achieve the same result.