0% found this document useful (0 votes)
5 views1 page

Python Type Casting CheatSheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Python Type Casting CheatSheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

■ Python Type Casting Cheat Sheet

Type Casting (or Type Conversion) means converting one data type into another. Python supports
several built-in functions for this.

Function Description Example

int(x) Converts x to an integer int("5") → 5


float(x) Converts x to a float float("3.14") → 3.14
str(x) Converts x to a string str(10) → "10"
bool(x) Converts x to a boolean bool(0) → False
list(x) Converts x to a list list("abc") → ['a', 'b', 'c']
tuple(x) Converts x to a tuple tuple([1, 2]) → (1, 2)
set(x) Converts x to a set set([1, 2, 2]) → {1, 2}

Notes: Strings that look like numbers (e.g. "10") can be cast to int or float. Empty values like "", [], 0
become False when cast to bool. Casting can fail if the format is invalid: e.g. int("hello") causes an
error.

You might also like