0

My python code is as follows :

class Student():

        def __init__(self, name, branch, year):
            self.name = name
            self.branch = branch
            self.year = year
            # Variables name,branch and year are instance variables
            # Different objects like Student1, Student2 will have diff values of these variables.
            print('Student object is created for Student : ', name)

       def print_details(self):
            print('Name:', self.name)
            print('Branch:', self.branch)
            print('Year:', self.year)

Stud1 = Student('AAkash','ECE',2015)
Stud2 = Student('Vijay','IT',2017)

Stud1.print_details()
Stud2.print_details()

My output is :

('Student object is created for Student : ', 'AAkash')
('Student object is created for Student : ', 'Vijay')
('Name:', 'AAkash')
('Branch:', 'ECE')
('Year:', 2015)
('Name:', 'Vijay')
('Branch:', 'IT')
('Year:', 2017)

whereas in my required output, i want statements like :

Name : AAkash
Branch : CSE
zvone
  • 18,045
  • 3
  • 49
  • 77
Jaskunwar singh
  • 205
  • 3
  • 5
  • 10

1 Answers1

1

you're using python-3.x print syntax but you are probably just running python 2.7 . print in python2 isn't a function it's a statement, it only looks like a function.

when you do

print('Name:', self.name)

you're telling the print statement to print a tuple with 2 items in it - and that is exactly what it does

you can just remove the parenthesis and have it look like so:

print 'Name:', self.name
print 'Branch:', self.branch
print 'Year:', self.year

which would print what you want (actually print multiple items not the tuple itself)

or

you can get the python3 style print function in your code by importing it from future (in python 2.7):

from __future__ import print_function

and it will give you your desired output

AntiMatterDynamite
  • 1,495
  • 7
  • 17