如何将换行符后的字符串的第一个字母大写?
How to uppercase the first letter of a string that follows a line break?
我有一个包含多个段落的长文本。它存储在一个变量中。
我需要将换行符(或句点)后的每个单词的首字母大写。
最简单的方法是什么?
我想您可以使用分隔符 \n 来拆分您的文本。所以代码应该是这样的:
output = []
for x in longString.split("\n"):
try:
x = x[0].upper() + x[1:]
except:
pass
output.append(x)
outputString = "\n".join(output)
通过这种方法,您可以将换行符后的第一个字母大写。您可以按照类似的方法处理句点。
如果有帮助请告诉我! :D
这样试试
可能有点复杂。
我好像重建了upper()函数
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)
结果如下
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
这是一个代码,将新行的每个单词的字母大写。
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
输出:
Hiii.
Hellooooooooo.
My name is stack.
Dhdjljdkd.
我有一个包含多个段落的长文本。它存储在一个变量中。
我需要将换行符(或句点)后的每个单词的首字母大写。
最简单的方法是什么?
我想您可以使用分隔符 \n 来拆分您的文本。所以代码应该是这样的:
output = []
for x in longString.split("\n"):
try:
x = x[0].upper() + x[1:]
except:
pass
output.append(x)
outputString = "\n".join(output)
通过这种方法,您可以将换行符后的第一个字母大写。您可以按照类似的方法处理句点。
如果有帮助请告诉我! :D
这样试试
可能有点复杂。
我好像重建了upper()函数
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)
结果如下
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
这是一个代码,将新行的每个单词的字母大写。
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
输出:
Hiii.
Hellooooooooo.
My name is stack.
Dhdjljdkd.