[PTA] 7-9 capitalize the first letter of the sentence (python)

Enter a line of sentences and capitalize the first letter of each sentence. There are spaces between every word and every sentence starts with ? or.or! end.

Input format:

Enter a string

Output format:

Capitalize the first letter of the sentence and output the string

Sample">Sample">Input sample:

you are a sight sore eyes! you look well.

Sample output:

You are a sight sore eyes! You look well. 

   The difficulty in my understanding of this topic lies in the space and the ability to enter multiple sentences, which can be separated according to the method of the topic, so one of my ideas is to find the separator first, and then judge whether there will be the content of the next sentence. If there is the next sentence Then save it in a list, and the ord() and chr() functions need to be used in the final output.

  First of all, we need to get the data using the input input string, and then create a list to save the first letter of the sentence to facilitate the output operation.

The code for this problem looks like this:

s=input()
s=s.lower()        #先将输入的字符串全部变为小写,然后再将其首字母大写
save=[]
print(chr(ord(s[0])-32),end='')    #利用函数将其先变为ASCII码-32变为大写字母再转换为字符输出
for i in range(1,len(s)):
    if s[i] in'?.!':            #判断输入的字符串是否存在结尾符号,再存入下一个开头的数据
        save.append(i+2)
    if i in save:                #判断该字符串是否可以分为多个句子
        print(chr(ord(s[i])-32),end='')
    else:
        print(s[i],end='')
print(" ",end='')    #很重要,该题目最后的结尾有一个空格如果少一句则会显示错误

The result after running is as follows:

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/126713825