1

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?

Vincent Roye
  • 2,751
  • 7
  • 33
  • 53
  • 1
    You can add [mcve] to the question. – Ch3steR Mar 23 '20 at 04:43
  • 1
    Your question might be [something to this effect](https://stackoverflow.com/questions/40595150/python-3-how-to-capitalize-first-letter-of-every-sentence-when-translating-fro). – metatoaster Mar 23 '20 at 04:44
  • You can also use regular expression to parse by multiple delimiters [split strings into words with multiple word boundary delimiters](https://stackoverflow.com/questions/1059559/split-strings-into-words-with-multiple-word-boundary-delimiters) – joepol Mar 23 '20 at 05:19

3 Answers3

1

Here is a code that capitalize the the letter of every word of a new line.

texts=''' hiii.
          hellooooooooo.
          my name is stack.
          dhdjljdkd.
      '''
res=[ i.strip().capitalize() for i in texts.split('\n')]
print('\n'.join(res))

#for i in texts.split('\n'):
#    res+=i.strip().capitalize()+'\n'
#print(res)   #--- WORKS

Output:

Hiii.
Hellooooooooo.
My name is stack.
Dhdjljdkd.
halfer
  • 19,824
  • 17
  • 99
  • 186
teddcp
  • 1,514
  • 2
  • 11
  • 25
  • 1
    Please refrain from asking for votes or solution acceptances on Stack Overflow. These will be given organically by question authors as they see fit (it is not mandatory for them to vote at all). You can _occasionally_ remind folks to do this if you suspect they don't know the accept/vote systems exist, but don't make a habit of it. Be here because you want to help people, not because of the unicorn points. – halfer Mar 23 '20 at 10:27
0

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

EnriqueBet
  • 1,482
  • 2
  • 15
  • 23
0

Try like this.

May be it is a little complex.

I seem to have rebuilt the upper() function

import re
content="i have a long text that contains multiple paragraphs.\nit is stored in one variable.\ni need to uppercase the first letter every word that follows a line break (or a period).\nwhat is the most simple way to do that?"


def your_function_name(content):
    new_res=[]
    res=re.split(r'[.?\n]',content)
    while  ""  in res:
        res.remove("")
    for con in res:
        if 61<=ord(con[0])<=96:
            new_con=con
            new_res.append(new_con)
        elif 97<=ord(con[0])<=123:
            new_con=chr(ord(con[0])-32)+con[1:]
            new_res.append(new_con)
        else:
            new_con=con
            new_res.append(new_con)
    return new_res


print("Transformed\n-----------------")
new_res=your_function_name(content)
for i in new_res:
    print(i)

results are as follows

Transformed
-----------------
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
moqin
  • 64
  • 8