Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Method not regocnized
#1
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
Reply
#2
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())
Recommended Tutorials:
Reply
#3
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."
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020