Python Typecasting Examples | Live Code Programming

Python Typecasting Examples

Typecasting in Python is converting one data type into another. It helps when you want to perform operations requiring matching data types.

Python में Typecasting का मतलब होता है एक data type को दूसरे data type में बदलना। जब operations के लिए data types मेल खाने चाहिए, तब यह काम आता है।

Example 1: Integer to Float
x = 10
print(float(x))

Output:

10.0

Converts integer 10 to float 10.0.

Integer 10 को float 10.0 में बदला गया।

Example 2: Float to Integer
y = 9.99
print(int(y))

Output:

9

Converts float 9.99 to integer 9 (truncates decimal).

Float 9.99 को integer 9 में बदला गया (decimal हटाया गया)।

Example 3: Integer to String
num = 100
print(str(num))

Output:

'100'

Converts integer 100 to string '100'.

Integer 100 को string '100' में बदला गया।

Example 4: String to Integer
s = '25'
print(int(s))

Output:

25

Converts numeric string '25' to integer 25.

String '25' को integer 25 में बदला गया।

Example 5: String to Float
s = '3.14'
print(float(s))

Output:

3.14

Converts string '3.14' to float 3.14.

String '3.14' को float 3.14 में बदला गया।