what if we want to capitalize each word in a sentence (string?), for example “today is happy coding saturday”??
text = " ".join([t.capitalize() for t in text.split()])
Should work now.
“today is happy coding saturday”.capitalize()
use capitalize() function on each word
capitalize() is a method, not a function.
I found the solution if someone is also interested in this question.
-> “today is happy coding saturday”.title()
–> but another problem that ‘is’ has been defined in python environment…
How can I include ‘is’ in the string … (I thought one problem is solved, then another one pops up… typical coding situation
If it is mentioned inside " " or ’ ’ then it is considered as a string
the problem has been solved
This solution of splitting a text to a list of words, capitalizing each word and joining them to a new string has a drawback. It doesn’t preserve the original white spaces. E.g. new lines, tabs, and multiple consecutive spaces are replaced by single spaces between words.
As already answered, use of “some text”.title() is an appropriate approach. Custom alternative implementations are encouraged - for learning purposes, of course.