Python Forum

Full Version: Method not regocnized
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone,

I started learning Python not too long ago, but unfortunatley I ran into some trouble with using a String Method.

This is my code:

 

famous_person = 'bruce lee'
message ='famous_person.title()' +  'once said,' + '"the more we value things, the less we value ourselves."'
print(message)


Error:
famous_person.title() once said, "the more we value things, the less we value ourselves."'
famous_person.title() doesn't change to the value I gave it. (bruce lee.) What am I doing wrong?

Greetings Indra
Your string method is within the quotes making it apart of the string instead of a string method

This fixes your problem, however you shouldn't use concatenation. Its so unpythonic.
message ='{}'.format(famous_person.title()) +  'once said,' + '"the more we value things, the less we value ourselves."'
such as:
message ='{} once said, "the more we value things, the less we value ourselves."'.format(famous_person.title())
If using 3.6 <= Python then you can consider f-strings as well:

>>> famous_person = 'bruce lee'                                            
>>> message = f'{famous_person.title()} once said, "the more we value things, the less we value ourselves."'                                      
>>> print(message)                                                         
Bruce Lee once said, "the more we value things, the less we value ourselves."
Thank you for all your solutions.
About the concatention, this was kind of the point. I was learning about concatention, but they did mention that it's not too Python-like.

Thanks for your help!