0

What I'm trying to do is to get some text from the user store it in a variable. I then split that variable by a period which gives me a sentence_list. Then I want to capitalize the starting word of each element contained in the list.

userText = input('Enter your text: ') sentence = userText.split('. ')

GOD
  • 33
  • 9
  • 2
    "I then split that variable by a period which gives me a sentence_list." Are you sure of that? Related: [Python split text on sentences](https://stackoverflow.com/q/4576077/1639625) – tobias_k Mar 23 '18 at 14:19
  • `>>> 'i am a robot'.title()` produces `'I Am A Robot'`. `>>> 'i am a robot'.capitalize()` produces `'I am a robot.'`. Beep bop. – Alex Huszagh Mar 23 '18 at 14:21
  • To capitalize the first character of strings in a list: `cap_list = [x[0].upper() + x[1:] for x in sentence]`. You may want to call `strip()` on it to strip whitespace. – whp Mar 23 '18 at 14:21
  • 2
    @whp Don't actually do that. Python has a built-in tool for that already. Don't reinvent the wheel. – Alex Huszagh Mar 23 '18 at 14:22
  • If you want to capitalize the first letter in each str, use `capitalize`. To capitalize the first letter in each word, use `title`. Read Python's [documentation](https://docs.python.org/3/library/stdtypes.html#str.capitalize). – Alex Huszagh Mar 23 '18 at 14:24
  • @whp, EDIT: it works! – GOD Mar 23 '18 at 14:29
  • 1
    @GOD - sentence should be a list of sentences (same as the variable `sentence` in your post), not a string. Also, use `capitalize` as it was pointed out. `[x.capitalize() for x in sentence]` – whp Mar 23 '18 at 14:32
  • @GOD, no it does not. `title()` does capitalize every word in the string, `capitalize()` only capitalizes the first word and only if it begins with alphabetical character. – Alex Huszagh Mar 23 '18 at 14:35

0 Answers0