calthoff / tstp

This is an old repository for the exercises in "The Self-Taught Programmer." Please see /selftaught.
166 stars 193 forks source link

Title vs capitalize? #1

Closed cogginproductions closed 7 years ago

cogginproductions commented 7 years ago

In the book you talk about using capitalize() for capitalizing the first letter of a sentence, but in the solution file you show using title(). And in the challenge question you have "Huxley" with a capital "H" but in the solution its lower case.

Now when I use capitalize, it makes the first letter of the sentence capital but makes every other words first letter lower case (i.e. "aldous Huxley was born in 1984." becomes "Aldous huxley was born in 1984.").

Then when I use title(), "aldous Huxley was born in 1984." becomes "Aldous Huxley Was Born In 1984."

I'm a little confused on what's going on....

jonathan-j-stone commented 7 years ago

I'm with you on this, @cogginproductions. From the challenge question in my version of the book, I thought the answer was: "aldous Huxley was born in 1894.".capitalize()

but the code returned:

'Aldous huxley was born in 1894.'

What gives?

calthoff commented 7 years ago

Thanks for the feedback. I am looking into this now.

calthoff commented 7 years ago

@ILeftTheLaw @cogginproductions I didn't even realize this is the repository for the old version of the book. Please email me so I can get you the latest version. This problem should be resolved there. Very sorry about that.

calthoff commented 7 years ago

@cogginproductions @ILeftTheLaw I looked into this. You both are correct. capitalize() returns a new string and makes the rest of the string lowercase. I forgot that it also makes everything else in the string lowercase. I will change this example. Thanks so much for pointing this out. In the meantime, here is one way you could solve the challenge:

def upperfirst(string):
    return string[0].upper() + string[1:]

print(upperfirst("aldous Huxley was born in 1894."))
cogginproductions commented 7 years ago

You're welcome and thank you. I will give it a try. Glad I can help out!