4

For example, I have this sentence:

hello. my name is Jess. what is your name?

and I want to change it into:

Hello. My name is Jess. What is your name?

I came up with this code, but I have one problem with connecting everything back together

def main():
    name = input('Enter your sentence: ')
    name = name.split('. ')
    for item in name:
        print (item[0].upper() + item[1:], end='. ')

When I put in the sentence, it will return:

Hello. My name is Jess. What is your name?.

How can I stop the punctuation from appearing at the end of the sentence? Also, what if I have a question in the middle, for example:

hi. what is your name? my name is Jess.
hn_03
  • 97
  • 1
  • 7

6 Answers6

8

This one is better solution

x = "hello. my name is Jess. what is your name?"
print( '. '.join(map(lambda s: s.strip().capitalize(), x.split('.'))))

output:
Hello. My name is jess. What is your name?
Ramesh K
  • 282
  • 4
  • 13
  • It makes the name `Jess` become lowercase. OP requested output be `Hello. My name is Jess. What is your name?` – Wayne Dec 22 '20 at 23:12
2

You can construct your string first, following the same procedure, and the showing the string, except the last two values (you also need to remove the last space):

def main():
    result = ""
    name = input('Enter your sentence: ')
    name = name.split('. ')
    for item in name:
        result += item[0].upper() + item[1:] + '. '
    print result[:-2] 
Leo
  • 1,174
  • 11
  • 25
  • or even better combine this with @AdamHughes answer – Joran Beasley Feb 20 '15 at 23:02
  • I don't know why when I did it like that, there's an Invalid syntax exception raised, specifically at the end=' .' part – hn_03 Feb 20 '15 at 23:10
  • Importantly, this solution doesn't make proper names internal to the sentences lowercase. A more general solution for all typical end-of-sentence punctuation can be found at https://stackoverflow.com/a/63192453/8508004 . – Wayne Dec 23 '20 at 00:10
2

As an alternative (and may be as an over complication also), you can use nltk's Sentence Segmentation. Relying on @J.F. Sebastian's answer:

>>> import nltk.data
>>> 
>>> sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
>>> 
>>> text = "hello. my name is Jess. what is your name?"
>>> 
>>> sentences = sent_tokenizer.tokenize(text)
>>> sentences = [sent.capitalize() for sent in sentences]
>>> print(' '.join(sentences))
Hello. My name is jess. What is your name?

Why it is not as simple as splitting a string by .? Generally speaking, the problem with . is that it is not only serving as a sentence delimiter. It also can be a part of an acronym or mark abbreviations inside a sentence (think also about all of the Mr., Mrs., Dr. etc):

Sentence segmentation is difficult because period is used to mark abbreviations, and some periods simultaneously mark an abbreviation and terminate a sentence, as often happens with acronyms like U.S.A.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Am I overlooking the obvious or can you just right strip?

Ie before you output your string:

finalstring = string.rstrip('.')
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
0

I have written a solution to similar question, you can read how the following codes works here: https://stackoverflow.com/a/48688182/5265414

   original_data = raw_input("Enter text: ")
    list = original_data.split(".")
    if original_data.endswith('.'):
        list.remove('')

    for w in list:
        stripper= w.strip().capitalize() +"."
        print stripper,
devmike01
  • 1,971
  • 1
  • 20
  • 30
0

I have recently come across this string.capsword and found it useful.

import string
sentence = 'hello. my name is Jess. what is your name?' 
string.capsword(sentence, sep='. ')  # sep should be a dot and space
John Prawyn
  • 1,423
  • 3
  • 19
  • 28