Representation of phone number in USA format: (xxx) xxx-xxxx. : to string « Class « Python Tutorial






class PhoneNumber:
   def __init__( self, number ):
      self.areaCode = number[ 1:4 ]
      self.exchange = number[ 6:9 ]
      self.line = number[ 10:14 ]  

   def __str__( self ):
      return "(%s) %s-%s" % ( self.areaCode, self.exchange, self.line )

newNumber = "(123) 456-7890"

phone = PhoneNumber( newNumber )  
print "The phone number is:",
print phone








11.31.to string
11.31.1.Simple Customization
11.31.2.Basic Customization
11.31.3.Numeric Customization
11.31.4.Representation of phone number in USA format: (xxx) xxx-xxxx.