B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Type Casting | LiveCodeProgramming

Python Type Casting

Type casting in Python means converting a value from one data type to another. For example, changing a float to an integer or a number to a string. Python gives you built-in functions like int(), float(), str(), and bool() to do this.

Python में Type Casting का मतलब होता है एक value को एक data type से दूसरे data type में बदलना। जैसे एक float को integer में बदलना या number को string में बदलना। Python में int(), float(), str(), bool() जैसे built-in functions होते हैं।

Example 1: String to Integer
x = "123"
y = int(x)
print(y, type(y))

Output:

123 <class 'int'>

Convert a numeric string to integer.

एक string को integer में बदला गया है।

Example 2: Float to Integer
x = 7.99
y = int(x)
print(y, type(y))

Output:

7 <class 'int'>

Float to integer removes decimal part.

Float को integer में बदलने पर दशमलव हट जाता है।

Example 3: Integer to Float
a = 10
b = float(a)
print(b, type(b))

Output:

10.0 <class 'float'>

Converts integer to float.

Integer को float में बदला गया है।

Example 4: Number to String
num = 500
text = str(num)
print(text, type(text))

Output:

500 <class 'str'>

Number to string.

Number को string में बदला गया है।

Example 5: Value to Boolean
print(bool(0))
print(bool(1))
print(bool(""))
print(bool("Hello"))

Output:

False
True
False
True

0, empty string → False; others → True.

0 और खाली string → False; बाकी सब → True।