I was using the capitalize method on some strings in Python and one of strings starts with a space:
phrase = ' Lexical Semantics'
phrase.capitalize()
returns ' lexical semantics' all in lower case. Why is that?
capwords() capwords() is a python function that converts the first letter of every word into uppercase and every other letter into lowercase. The function takes the string as the parameter value and then returns the string with the first letter capital as the desired output.
The capitalize() method returns a string where the first character is upper case, and the rest is lower case.
The upper() method converts all lowercase characters in a string into uppercase characters and returns it.
You can use str. capitalize() to capitalise each string. If you have any other uppercase letters in the string they will be lowered which may or may not be relevant.
This is the listed behaviour:
Return a copy of the string with its first character capitalized and the rest lowercased.
The first character is a space, the space is unchanged, the rest lowercased.
If you want to make it all uppercase, see str.upper()
, or str.title()
for the first letter of every word.
>>> phrase = 'lexical semantics' >>> phrase.capitalize() 'Lexical semantics' >>> phrase.upper() 'LEXICAL SEMANTICS' >>> phrase.title() 'Lexical Semantics'
Or, if it's just a problem with the space:
>>> phrase = ' lexical semantics' >>> phrase.strip().capitalize() 'Lexical semantics'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With