Capitalization of each sentence in a string in Python 3

后端 未结 5 1140
-上瘾入骨i
-上瘾入骨i 2021-01-22 10:25

This should be easy but somehow I\'m not quite getting it.

My assignment is:

Write a function sentenceCapitalizer that has one parameter of type

5条回答
  •  旧巷少年郎
    2021-01-22 10:45

    I did not use 'split' but just while loop instead. Here is my code.

    my_string = input('Enter a string: ')
    new_string = ''
    new_string += my_string[0].upper()
    i = 1
    
    while i < len(my_string)-2:
        new_string += my_string[i]
        if my_string[i] == '.' or my_string[i] == '?' or my_string[i] == '!':
            new_string += ' '
            new_string += my_string[i+2].upper()
            i = i+3
        else:
            if i == len(my_string)-3:
                new_string += my_string[len(my_string)-2:len(my_string)]
            i = i+1
    
    print(new_string)
    

    Here is how it works:

    Enter a string: hello. my name is Joe. what is your name?
    Hello. My name is Joe. What is your name
    

提交回复
热议问题