python cast
Python hosting: Host, run, and code Python in the cloud!
Python determines the datatype automatically, to illustrate:
|
It finds x is of type integer and y of type string.
Functions accept a certain datatype. For example, print only accepts the string datatype.
Related Course:
Python Programming Bootcamp: Go from zero to hero
Datatypes casting
If you want to print numbers you will often need casting.
In this example below we want to print two numbers, one whole number (integer) and one floating point number.
x = 3 |
We cast the variable x (integer) and the variable y (float) to a string using the str() function.
What if we have text that we want to store as number? We will have to cast again.
a = "135.31421" |
In the example above we cast two variables with the datatype string to the datatype float.
Conversion functions
To convert between datatypes you can use:
Function | Description |
---|---|
int(x) | Converts x to an integer |
long(x) | Converts x to a long integer |
float(x) | Converts x to a floating point number |
str(x) | Converts x to an string. x can be of the type float. integer or long. |
hex(x) | Converts x integer to a hexadecimal string |
chr(x) | Converts x integer to a character |
ord(x) | Converts character x to an integer |
Leave a Reply:
hello
I didn't understand the significance of casting. I intend I could have directly give the command print c to print the value of c in example 1. Why did I opt to use datatype casting ?
what is the difference between the usage of single inverted commas ( ' ' ) and double inverrted commas ( " " ).
thanks
There are situations in which casting when printing is neccesary. If you want to print integers in combinations with strings or in many other situations. The casting will convert say the integer to a string for printing. If you can print without casting, you should, but this is not always the case.
Both the single commmas (') and double commas (") are equal and what you use is what you prefer.
Why we need to insert + before str(x) on the following example:
print "x = " + str(x)
thanks
Hi, the + is for concatination, it adds two strings together. Print takes only one argument, thus we combine the string "x=" and str(x) into one string with the + operator.
Some more details on datatype casting (int to string):
Back in the early days of computing they had only binary numbers (0s or 1s) and wanted a way to print characters to the screen. There was no such way and they made a method that maps binary numbers to latin characters known as the ASCII table. In this table every byte (8 bits) is a character. This means if you print the number 102842 you print individual bytes '1', '0', '2', '8', '4' and '2' in ASCII representation to the screen. An integer is a raw block of memory, usually only 4 bytes and can hold this same value in those 4 bytes.
Let's do an example. The value 128 can be stored in a single byte using this code: print int('10000000', 2). This byte would take just 8 bits (1s or 0s) in computer memory. To print it, Python uses the ASCII table. Every character is expressed as 1 byte. That means to show the same value on the screen, the computer needs 1 byte for '1', '2' and '8'.
To print the ASCII table you can use this code:
In the above example, instead if I try the below statement, what will happen?
Hi,this will output the sum of both if you use the quote (") and python 2.7
sum2 = int(num) + int(flt)
print sum2.
I tried this and got an error message. Is anything wrong in this?
Hi, which error message did you get? Are you using python on desktop or on the web?
This code works on my machine (Python 2.5, Python 2.7):
for Python 3 it works with:
In the paragraph: An example of casting datatypes in Python:
There is a typo in the sentence "In this example below we weant to print"
I really like this tutorial thanks for making it!
Thanks Mason! I updated it
In print "x = " + str(x) couldn't we replace the "+" for a comma?
Hi Bruno, thanks for your comment! This outputs the same but does not concatenate the string data. If you only want to ouput, this is fine. If you want to do operations on the string later, you can store the output in a string value that way.
Thank you
Nice example