1

Basically as the title says, I want the sentences of the user input to be capitalized, but not lose their capitalization in the process. The input's supposed to be two sentences that get separated by periods. The code I have here outputs them the sentences, but not joined or keeping the rest of the capitalization.

def main():

 user_input = input("Enter the sentence you would like to be modified!: ").split(". ")
 capitalized_sentences = [user_input[0].upper() + user_input[1:] for sentence in user_input]
 recombined_sentences = ". ".join(capitalized_sentences)
Ean Crosby
  • 19
  • 3
  • Do you mean you only want the first word of each sentence capitalized? What you have should do that. Show us some sample input, and what you got, and what you expected. – Tim Roberts Apr 09 '21 at 00:15
  • 4
    Does this answer your question? [Capitalize the first letter of a string without touching the others](https://stackoverflow.com/questions/31767622/capitalize-the-first-letter-of-a-string-without-touching-the-others) – PacketLoss Apr 09 '21 at 00:17
  • When I run what I have, it de-capitalizes everything else the user has inputted. Such as, if someone where to put in "my favorite character is Charlie Brown. he is a great sport.", it'll capitalize the sentences, but remove the capitalization on Charlie Brown. I want it to keep all other capitalization as well as capitalizing the first word of every sentence. – Ean Crosby Apr 09 '21 at 00:22
  • So you don't really want `capitalize()`. You just want to replace the first character with its `.upper()`. Is that enough of a hint? – Tim Roberts Apr 09 '21 at 00:24

2 Answers2

2

Just edit the first character of each split to be upper:

# For this example, lets use this string. However, you would still use
# user_input = input("...") in your actual code
user_input = "for bar. egg spam."

# Turn the user_input into sentences.
# Note, this is assuming the user only is using one space.
# This gives us ["foo bar", "egg spam"]
sentences = user_input.split(". ")

# This is called a list comprehension. It's a way of writing
# a for-loop in Python. There's tons of documentation on it
# if you Google it.
#
# In this loop, the loop variable is "sentence". Please be mindful
# that it is a singular form of the word sentences.
#
# sentence[0].upper() will make the first letter in sentence uppercase
# sentence[1:] is the remaining letters, unmodified
#
# For the first iteration, this becomes:
# "f".upper() + "oo bar"
# "F" + "oo bar"
# "Foo bar"
capitalized_sentences = [sentence[0].upper() + sentence[1:] 
                         for sentence 
                         in sentences]

# At this point we have ["Foo bar", "Egg spam"]
# We need to join them together. Just use the same ". " we
# used to split them in the beginning!
#
# This gives us "Foo bar. Egg spam."
recombined_sentences = ". ".join(capitalized_sentences)

Replace "sentences" with your user_input bit

Note, there might be a "gotcha" if the user inputs sentences of a format you aren't expecting. For example, what if the user entered two spaces instead of one? Then the above code would try to capitalize a whitespace character. You would need to account for that.

  • 1
    I tried using this setup with my own tweaks, but it gives me a type error, and I'm not quite sure why. "capitalized_sentences = [user_input[0].upper() + user_input[1:] for sentence in user_input] TypeError: can only concatenate str (not "list") to str – Ean Crosby Apr 09 '21 at 00:44
  • what does `type(user_input)` give you? show me what you've done by editing your question – coffeepenbit Apr 09 '21 at 00:57
  • I've changed my code in my question. I can't get it to pull the type without it crashing. – Ean Crosby Apr 09 '21 at 01:06
  • The issue is that you did not properly adapt my code. it should be `sentence[0].upper()` and `sentence[1:]`. You only need to replace the last part of that second line, i.e. sentence**s**, with `user_input`. Emphasis on singular vs plural. Sentence**s** represents the list of sentences, whereas sentence represents a single sentence string. Let me know if you need more clarification. – coffeepenbit Apr 09 '21 at 01:30
  • 1
    I changed it around and got it to work! Thank you so much, I've been stuck on this for an unreasonable amount of time. Is there a chance you could explain what's happening in this code so I know for next time? – Ean Crosby Apr 09 '21 at 01:36
  • If it worked for you, make sure to mark it as the answer so that other people visiting will know in the future. I'll edit my answer to explain it better. – coffeepenbit Apr 09 '21 at 01:40
-1

It's very simple: You can use the String method upper() on a part of the string.

Here is a one-liner to do just that:

CapWord = "".join([c.upper() if i == 0 else c for i, c in enumerate([j for j in rawWord])])

Just replace CapWord and rawWord with respective values (you can change them to sentences / words depending on what you want to do.

What it does:

  • Iterates over an array with all the characters in the string and their respective enumeration (to avoid duplicate letters being capitalised as well) then checks if the char (c) has a number corresponding to the indexes to capitalise, and is converted to a string.
Manu1800
  • 145
  • 7
  • It's still a bad solution, because you're wastefully processing every letter, when you know you're only going to tweak the first. – Tim Roberts Apr 09 '21 at 00:52
  • Absolutely true, the function would've been carefully optimized if OP asked for a more complex but efficient solution, this is simple(r). – Manu1800 Apr 09 '21 at 01:04