-2

I am new to Python.

How do we write this. My out put is not correct.

Programming challenge description:

Write a program which capitalizes the first letter of each word in a sentence.

Input: Your program should read lines from standard input. Each line has a sequence of words.

Output: Print the capitalized words.

Test 1

Test InputDownload Test 1 Input

Hello world

Expected Output:

Hello World

A Letter

import sys
# import numpy as np
# import pandas as pd
# from sklearn import ...

for line in sys.stdin:
    print(line, end="")
    line = "Hello world"
tripleee
  • 175,061
  • 34
  • 275
  • 318

2 Answers2

0

To capitalize a word use your_word.capitalize()

If you need to capitalize each word of a sentence like "Hello world!" you first need to split your sentence in a list of word:

sentence = "Hello world!"
words = sentence.split(" ")

Then, Modify the list capitalizing each word.

words = [word.capitalize() for word in words]

And finally convert you list back to a string to get the wanted result

result = " ".join(words)

For the 2nd example if you just need to capitalize the first word, instead of using the list comprehension words = [word.capitalize() for word in words] you can just mutate the first element of the list, without reassigning it entirely

words[0] = words[0].capitalize()

Here is a concrete example using input

def capitalizeSentence(s):
    return " ".join([w.capitalize() for w in s.split(" ")])

inputSentence = input("type a sentence")
print(capitalizeSentence(inputSentence))

EDIT

As said in the comment below, the `str.title() method that I didn't know, allows you to capitalize each word in a sentence directly:

inputSentence = input("type a sentence")
print(inputSentence.title())
Peterrabbit
  • 2,166
  • 1
  • 3
  • 19
  • for line in sys.stdin: print(line, end="") line = "Hello world" words = line.split("") words = (word.capitalize() for word in words) result = "". join(words) break words(0) = words (0).capitalize() – Aye Chan Thu Mar 31 '22 at 08:09
  • Gives me this error – Aye Chan Thu Mar 31 '22 at 08:09
  • Test 1 Failed Test Input: Hello world Expected Output: Hello World Your Output: (None) stderr File "/tmp/source.py", line 13 words(0) = words (0).capitalize() ^ SyntaxError: cannot assign to function call Test 2 Failed Test Input: a letter Expected Output: A Letter Your Output: (None) stderr File "/tmp/source.py", line 13 words(0) = words (0).capitalize() ^ SyntaxError: cannot assign to function call – Aye Chan Thu Mar 31 '22 at 08:09
  • you are trying to index with parenthesis `words(0)`, it's `words[0]`. (please avoid posting too much code in comments, it's unreadable) – Peterrabbit Mar 31 '22 at 08:11
  • 1
    Instead of running `.capitalize()` on each word in a string, you can just use `sentence.title()` and it will capitalize the first letter of every word. – David Buck Mar 31 '22 at 15:30
  • @DavidBuck Didn't know that! I added it to the examples. Thanks. – Peterrabbit Apr 01 '22 at 07:50
0

This is my version with some comments to help:

# Write a program which capitalizes the first letter of each word in a sentence.


sentance = 'hello and have a nice day'
new_sentance = ''

# split the sentance
words = sentance.split(' ')

# make each first letter uppercase
for word in words:
    new_sentance = new_sentance + ' ' + word.capitalize()



print(new_sentance)

result:

Hello And Have A Nice Day
D.L
  • 4,339
  • 5
  • 22
  • 45
  • I follow what you suggested but still got the out put as " Your Output: Hello world Hello Hello World" – Aye Chan Thu Mar 31 '22 at 11:21
  • @AyeChanThu how do you mean ? an example of the input string `hello and have a nice day` is given and the output from this `Hello And Have A Nice Day` is also shown... – D.L Mar 31 '22 at 12:06