Hacker News new | ask | show | jobs
by macbony 4517 days ago
I prefer

  print("A {0} can be very cute!".format(pet))
.format() is very versatile.

  d = {'first': 'Robert', 'last': 'Paulson'}
  print("His name was {first} {last}!".format(**d))
  >> His name was Robert Paulson!


  class Person:
      def __init__(self, first, last):
          self.first, self.last = first, last

  p = Person('Robert', 'Paulson')
  print("His name is {0.first} {0.last}!".format(p))
  >> His name was Robert Paulson!
You also do not need to know what type is being passed in as the __str__ method is used for .format().