Python Forum
Definition of .format? (Newbie question)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Definition of .format? (Newbie question)
#1
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 : { } .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?
Reply


Messages In This Thread
Definition of .format? (Newbie question) - by Jan_97 - Jul-12-2019, 07:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 294 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  mutable argument in function definition akbarza 1 511 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 730 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  error occuring in definition a class akbarza 3 752 Nov-26-2023, 09:28 AM
Last Post: Yoriz
  newbie question - can't make code work tronic72 2 718 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 644 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 610 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 1,001 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,223 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  [split] Explain the python code in this definition Led_Zeppelin 1 767 Jan-13-2023, 10:20 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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