Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print type of variable in python3.5 on django 1.8

I am having a hard time of determining the type of my variable since I am used on python2 and have just migrated to python3

from django.http import HttpResponse

def myview(request):
    x = "Name"
    print (x)
    print type(x)
    return HttpResponse("Example output")

This code will throw an error because of print type(x). However if you changed that syntax line to type(x). The type does not return an output on the runserver of django.

like image 517
Dean Christian Armada Avatar asked Jan 05 '16 03:01

Dean Christian Armada


People also ask

How do you print the data type of a variable in Python?

How to Print the Type of a Variable in Python. To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How to print function in Python?

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Where does Django print to?

If you are using apache2 server to run django application and enabled access & error logs, your print statements will be printed in the error logs. mostly apache2 logs will be in this location /var/log/apache2/ .


1 Answers

print in Python 3 is no longer a statement, but a function. You need to call it using parentheses:

print(type(x))
like image 151
knbk Avatar answered Sep 19 '22 18:09

knbk