I'm completly new to python and I've only been learning it for about a week. I have over the last couple of days spent many hours watching tutorials and creating my own codes but there's this one thing I'm a little stuck upon. I will explain when we get there.
I'm having problems understanding how the .format works and what its uses are.
by definition I found this explanation.
Syntax :
Parameters :
(value) : Can be an integer, floating point numeric constant, string, characters or even variables.
Returntype : Returns a formatted string with the value passed as parameter in the placeholder position.
This all seems well and good. However, in my partially self coded class example I can't figure out what the .format is used for.
Here is my code.
Why is
Maybe I'm just very stupid, but I can't make sense of my own stuff. I just know I had to include it....
In this class however, I don't have .format included and it runs almost just the same.
I'm having problems understanding how the .format works and what its uses are.
by definition I found this explanation.
Syntax :
{ } .format(value)Parameters :
(value) : Can be an integer, floating point numeric constant, string, characters or even variables.
Returntype : Returns a formatted string with the value passed as parameter in the placeholder position.
This all seems well and good. However, in my partially self coded class example I can't figure out what the .format is used for.
Here is my code.
class meaning:
def __init__(self,name,lastname,nickname):
self.name = name
self.lastname = lastname
self.nickname = nickname
def myfunc(self):
return "{} {} {}".format(self.name, self.lastname,self.nickname)
dude1 = meaning ("John", "Cena" ,"The hulk")
dude2 = meaning ("Gordon", "Freeman", "MRMIME")
print (dude1.myfunc())
print (dude2.myfunc())My question here is.Why is
.format necessary here? I found a tutorial suggesting I need to have it included and that I can't just use str instead. However, there are no digits in this function. Why do I need .format if it's just text?Maybe I'm just very stupid, but I can't make sense of my own stuff. I just know I had to include it....
In this class however, I don't have .format included and it runs almost just the same.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print ("Hello, my name is " + self.name)
p1 = Person ("Pyke", 36)
p1.myfunc()
print(p1.name)
print(p1.age)What have I missed?
