【问题标题】:Lowercase the first letter of the first word of each sentence in a paragraph将段落中每个句子的第一个单词的第一个字母小写
【发布时间】:2022-11-27 04:17:02
【问题描述】:

如何将段落中每个句子的第一个单词的首字母小写?此外,句子中间的名词将保持大写。

我怎样才能在 Python 中做到这一点?

例如:“这是一个例句。请帮帮我。我不想要这种情况。在柏林我们玩得很开心。”到“这是一个例句。请帮助我。我不想要这种情况。在柏林我们玩得很开心。”

我试过这个,但这只是一个较低的句子。

【问题讨论】:

  • 请阐明您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python string


【解决方案1】:

我会为此使用正则表达式:

import re

s = "This is a example sentence. Please help me. I don't want this situation. In Berlin we have a great time."

out = re.sub(r'((?:^|.)s*w+)', lambda m: m.group().lower(), s)

输出:"this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time."

regex demo

【讨论】:

  • 这在我的导航器中不起作用:(
  • @ulmo python 代码如何依赖于“航海家“。你到底在做什么?
【解决方案2】:

代码:

def first_lower(s):
    paragraph = ''
    _sentences = s.split('.') # split sentences from paragraph using .
    for sentence in _sentences:
        if sentence == '':
            continue
        paragraph += sentence[0].lower() + sentence[1:] + '.' # append converted string to paragraph
    
    return paragraph

string = """this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time."""
print(first_lower(string))

输出:

这是一个例句。请帮我。我不想要这种情况。在柏林,我们玩得很开心。

【讨论】:

  • 在您编写的代码中,它仅将第一个句子中第一个单词的第一个字母小写。当你写这个句子时,你使用了所有小写字母。但我的句子以大写字母开头
猜你喜欢
  • 2018-03-06
  • 2013-08-13
  • 2011-07-20
  • 1970-01-01
  • 2014-07-11
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多