Click here to Skip to main content
14,209,923 members
Rate this:
Please Sign up or sign in to vote.
See more:
Write a function that accepts a sentence and capitalize the first letter of each words in the string. You must use split() method to split the sentence into its words

s = caps first letter
Outcome: Caps First Letter

I'm thinking is there any other way to concat the words in print()?

Thank you.

What I have tried:

s = "caps first letter"
str = ""
for word in s.split():
    str = str + " " + word.capitalize()
print(str.lstrip())
Posted
Updated 11-Jun-19 22:43pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Yes, there is.

If you have a list, let's call it words, then you can use print to print those words separated by spaces:
print(*words)
The * operator in this context makes sure that each item in the list gets passed as a different argument to print. So, if words is ['Caps', 'First', 'Letter'], then the code expands to:
print('Caps', 'First', 'Letter')
And because print with multiple arguments prints out each argument separated by spaces, this does exactly what you wanted.

If you would not use the * operator, then the code would expand to this:
print(['Caps', 'First', 'Letter'])
which prints:
['Caps', 'First', 'Letter']
and that's clearly not what you want.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190617.3 | Last Updated 12 Jun 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100