Welcome to Serpia's blog!
Blog Entry
Capitalize every first word
Capitalize
every first word of a sentence in a string
Turn:
mystring = "dwarf nebula was a wicked little dwarf. he was not unhappy.
he had a little red volkswagen."
into:
Dwarf nebula was a wicked little dwarf. He was not unhappy. He had a little red volkswagen.
Solution:
Explanation:
In this line we see three string methods
at work:
join, capitalize and split. Other than that we also use a list
comprehension.
Yes, that's right! The split methods
return a list and in our case it uses ". " as the delimiter. Now for
every time this happens (hence for s in
), every element of the list (which is
really a bunch of strings will be capitalized:
The thing that happens here (the
interaction between capitalize and split) is called a list
comprehension, very cool stuff!
The join method turns the list into a
string again, using ". " as the delimiter. Done!
Comments
Post a comment