Python 3.4 - Formatting String Input into Title format

故事扮演 提交于 2020-01-05 08:11:19

问题


I'm a bit of a python amateur and I'm creating an extremely basic program. I need it to format a user's input into title format like so:

Input: hello

Output: Hello

This is what I have so far:

firstNameInput = input("Hello! What is your first name? \n")

firstName = firstNameInput.title

When I come to print firstName I get no error, however instead of printing firstName it prints:

<built-in method title of str object at 0x0000000003EA60D8>

Thanks for any help in advance! :)


回答1:


firstNameInput.title() you are missing parens

In [1]: s = "hello world"

In [2]:print (s.title)
Out[2]:<built-in method title of str object at 0x7fbea30830b0>

In [3]: s.title()
Out[3]: 'Hello World'

The first is a reference to the method, the second is actually calling it.



来源:https://stackoverflow.com/questions/24717603/python-3-4-formatting-string-input-into-title-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!