0

I am currently doing a task but I am stuck at the moment, so here is the code so far I have written:

string = input('please enter a string: ')
s = []

def sentenceCapitalize(string):
    strn = string.split('. ')   #convert to a list
    for x in strn:
        y = x[0].upper()
        y += x[1:].lower()
        s.append(y)
        
    print('.'.join(s))

sentenceCapitalize(string)       

It only gets me the result as a list and period is disappeared

Unexpected Output:

enter image description here

Expected Output:

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

And here is the question from the book:

Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

Can you fix this solution? thanks.

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
MINO
  • 126
  • 1
  • 9
  • 1
    https://www.geeksforgeeks.org/python-program-to-convert-a-list-to-string/ – Wang Liang Dec 28 '20 at 07:26
  • 1
    `print('. '.join(s))` – Nick Dec 28 '20 at 07:27
  • @KingStone I know that but i need the period back as well. for example, its only give me Hello My name is Joe What is your name? without the dot. how can i do that? – MINO Dec 28 '20 at 07:28
  • @Nick but the expected output is 'hello. my name is Joe. what is your name' with dots only. while the join() method get me ' hello, my name is Joe. what is your name?' its get me both comma and dot. – MINO Dec 28 '20 at 07:32
  • The output of the `print('. '.join(s))` is `Hello. My name is joe. What is your name?` – Nick Dec 28 '20 at 07:39
  • @Nick can you check my new edited question above? i just updated it... not sure whats going on... – MINO Dec 28 '20 at 07:42
  • Your input has a comma `,` after `hello`, not a period `.` But you are also lowercasing all the remaining letters in the string so you should remove the `.lower()` call. – Nick Dec 28 '20 at 07:49

2 Answers2

0

One sentence solution:

print('. '.join([st[0].upper() + st[1:] for st in string.split('. ')]))
Keijack
  • 778
  • 6
  • 12
0

The main three errors you have in your code are:

  • You convert to lower case the rest of the sentence, so for example Joe will become joe
  • You split based on ('. '), but when concatenating back you join by ('.'), so you are missing the white space
  • You need a regex split, to consider both , and .. In the regex you pass the two options separated by |, and for the case of dot you need to add before '' since '.' itself is an operator in regex. More about Regular Expression

Try this:

string = input('please enter a string: ')
s = []
import re

def sentenceCapitalize(string):
    strn = re.split(',|\.', string)   #convert to a list
    for x in strn:
        y = x[0].upper()
        y += x[1:]
        s.append(y)
        
    print('.'.join(s))

sentenceCapitalize(string)  
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
  • thanks! i didn't notice there is a name is inside it which is need to be capitalized. this is very important to take note. – MINO Dec 28 '20 at 07:53
  • @MINO actually there is still one part pending, I will edit the code, since the comma is not used in the split. I am editing the code now – Ignacio Alorre Dec 28 '20 at 07:54
  • What happens when the string is `hello, my Name is Joe. what is your name?`. I assume `Name` stays as uppercase `N` – Joe Ferndz Dec 28 '20 at 07:55