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.
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.
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.
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/ .
print
in Python 3 is no longer a statement, but a function. You need to call it using parentheses:
print(type(x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With