简体   繁体   English

(python):在每个点之后拆分为新行,并在保留点的情况下将每行的每个首字母大写

[英](python): split after each dot into a new line and capitalize each first letter of each line with keeping the dots

(python) I'm trying to get an input from the user (a sentence that contains many dots), and then split the sentence to a new line after each dot and capitalize every first letter of each line. (python)我正在尝试从用户(包含许多点的句子)获取输入,然后在每个点之后将该句子拆分为新行,并大写每行的每个第一个字母。

    s=input("enter a sentence with donts\n")
    csn=s.split(".")
    for i in csn:
        cap=csn.upper()
        print(cap)

input: 输入:

i love.python.it's. great.

output I love Python I'ts Great how can I keep the dots? 输出我爱Python我很好我如何保留点? like this 像这样

I love.
Python.
It's.
Great.

You can use str.capitalize to make the first chracter of each word a capital letter (if appropriate), and str.strip to remove excess spaces: 您可以使用str.capitalize将每个单词的第一个字符变成大写字母(如果适用),并使用str.strip去除多余的空格:

print('\n'.join([i.strip().capitalize() for i in s.split('.')]))

prints 版画

I love
Python
It's
Great

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM