Solution 1 - .capitalize () method
Use the .capitalize () method, regular expressions are not needed here.
>>> string = 'my favourite cool string' >>> string.capitalize() 'My favourite cool string''
Solution 2 - through regular expressions
This method will not be used by anyone in their right mind, many unnecessary actions only if, as a study of the possibilities of language and regular expressions
A few steps:
- You need to use the python re module's re.sub method to replace part of the string.
- We will replace the first character of the string that corresponds to the regular expression '^ \ w' - one character (digit or letter) at the beginning of the string.
- The replacement will be made with the same uppercase character (using the .upper () method for the first character of string [: 1].
>>> string = 'my favourite cool string' >>> import re >>> new_string = re.sub(r'^\w', string[:1].upper(), string) >>> new_string 'My favourite cool string'
Useful links for understanding regular expressions: one , two , three
And remember - "if a developer has decided to solve one problem through regular expressions, congratulations, he now has 2 problems."