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?
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?
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.
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
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