O.Kokla O.Kokla - 1 year ago 87
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 Source

You can use title():

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

Example from the console:

>>> 'jordan-bellfort image'.title()
'Joder-Bellfort Image'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download