Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UPPERCASE string to sentence case in Python

Tags:

python

How does one convert an uppercase string to proper sentence-case? Example string:

"OPERATOR FAIL TO PROPERLY REMOVE SOLID WASTE" 

Using titlecase(str) gives me:

"Operator Fail to Properly Remove Solid Waste" 

What I need is:

"Operator fail to properly remove solid waste" 

Is there an easy way to do this?

like image 979
MoreScratch Avatar asked Oct 11 '16 01:10

MoreScratch


People also ask

How do you convert a string from uppercase to lowercase in Python?

The lower() method converts all uppercase characters in a string into lowercase characters and returns it.

What does upper () do in Python?

The upper() method returns a string where all characters are in upper case. Symbols and Numbers are ignored.

How do you change uppercase to lowercase and lowercase in Python?

The swapcase() method returns the string by converting all the characters to their opposite letter case( uppercase to lowercase and vice versa).

How do you convert text to title case in Python?

The String title() method in Python is used to convert the first character in each word to uppercase and the remaining characters to lowercase in the string and returns a new string.


1 Answers

Let's use an even more appropriate function for this: string.capitalize

>>> s="OPERATOR FAIL TO PROPERLY REMOVE SOLID WASTE" >>> s.capitalize() 'Operator fail to properly remove solid waste' 
like image 107
brianpck Avatar answered Oct 02 '22 18:10

brianpck