O.Kokla O.Kokla - 3 months ago 12
Python Question

Capitalizing hyphenated name

I have a script looking like this:

firstn = input('Please enter your first name: ')
lastn = input('Please enter Your last name: ')
print('Good day, ' + str.capitalize(firstn) + ' ' + str.capitalize(lastn)+ '!')


It will work nicely with simple names like jack black or morgan meeman but when I input hyphenated name like
jordan-bellfort image
then I'd expect
"Jordan-Bellfort Image"
but I receive
"Jordan-bellfort Image"
.

How can I get python to capitalize the character right after hyphen?

Answer

You can use title():

print('Good day,', firstn.title(), lastn.title(), '!')

Example from the console:

>>> 'jordan-bellfort image'.title()
'Joder-Bellfort Image'