To make the first letter of a Python string uppercase and the remaining string lowercase, you can simply use the
capitalize() method, for example, like so:
str = "FOO BAR" cap_str = str.capitalize() print(cap_str) #=> "Foo bar"
When using the
capitalize() method, you should consider the following:
- It returns a new string with first letter capitalized and all remaining characters in lowercase (regardless of the case in the original string);
- It does not follow any grammar-specific capitalization rules (such as capitalizing the first letter at the start of each new sentence or after certain punctuations, etc.).
Consider, for example, the following, where there are two sentences in the string, but only the first letter of the entire string is capitalized and all remaining words are converted to lowercase:
str = "fOO BAR BAZ. Qux Qaz Quux." cap_str = str.capitalize() print(cap_str) #=> "Foo bar baz. qux qaz quux."
Hope you found this post useful. It was published . Please show your love and support by sharing this post.