Search code examples
pythonline-breaksuppercaseparagraph

How to uppercase the first letter of a string that follows a line break?


I have a long text that contains multiple paragraphs. It is stored in one variable.

I need to uppercase the first letter every word that follows a line break (or a period).

What is the most simple way to do that?


Solution

  • I suppose you could split your text using as separator character \n. So the code should look like this:

    output = []
    
    for x in longString.split("\n"):
        try:
            x = x[0].upper() + x[1:]
        except:
            pass
        output.append(x)
    
    outputString = "\n".join(output)
    

    With this approach you will be able to upper case the first letter after a line break. You can follow a similar approach for period.

    Let me know if this helps! :D