Python how all the words in a sentence uppercase first letter of transfer

How will all the words in the title of the first letter of a sentence in uppercase transfer

At work, we may have such a demand, text within the file are all lowercase, all of which need to be converted to uppercase first letter of the word, if not originally uppercase conversion, then how to do it?
Ado, code demonstrates:
demo_str = 'Hello Wo of RId'
S = demo_str.split ()
for I in Range (len (S)):
S [I] = S [I] .capitalize ()
Print ( '' .join (s))
the following steps:
first, the need to convert the sentence, as is assumed to be 'hello Wo Rld', end to end string has a space, there are a plurality of intermediate spaces, so, we first need to string at least one space, the removal of the first end of the space the way, is split method, split method returns a new list. Why is split way to do that? Because strings are immutable sequences, and now we have to change the contents of the string. The method of generating a list of split loops through each element, in this example, a hello, Wo, Rld, len ( s) 3, each element of the list were taken capitalized, i.e. capitalize method, then 'space' , join the new method is converted into a string.
Of course, the above method may be simplified, but the general idea has not changed, necessarily requires string -> variable sequence -> String This process of change. If wrong place, please correct me.

Published 13 original articles · won praise 0 · Views 306

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/104712514