Python Type Casting In Hindi

📔 : Python 🔗

किसी दी गयी value /variable को दूसरे type के variable में convert करना Type Casting कहते हैं। different - different values को किसी particular type में convert करने के लिए Python में predefined constructors use किये जाते हैं।

Python int Type Casting

किसी value को int / integer में convert करने के लिए int() का use किया जाता है। int() Boolean value True , False को 1 , 0 में convert करता है।

Python int Type Casting Example
Copy Fullscreen Close Fullscreen Run
print( int(1) )
print( int(12.6) )
print( int('1') )
print( int("90") )
print( int(True) )
print( int(False) )
Output
C:\Users\Rahulkumar\Desktop\python>python int_typacast.py
1
12
1
90
1
0

Note - int() use करके हम string और complex types की value को int में convert नहीं कर सकते हैं।

print( int(5j) ) #TypeError: can't convert complex to int
print( int('string') ) #ValueError: invalid literal for int() with base 10: 'string'

Python float Type Casting

value को float में convert करने के लिए float() constructor का use किया जाता है।

Python float Type Casting Example
Copy Fullscreen Close Fullscreen Run
print( float(1) )
print( float(12.6) )
print( float('43.6') )
print( float(True) )
print( float(False) )
Output
C:\Users\Rahulkumar\Desktop\python>python float_typecast.py
1.0
12.6
43.6
1.0
0.0

Python string Type Casting

value को string में convert करने के लिए str() constructor का use किया जाता है।

Python string Type Casting Example
Copy Fullscreen Close Fullscreen Run
print( str('string value') )
print( str(1) )
print( str(12.6) )
print( str(True) )
print( str(False) )
print( str(65j) )
Output
C:\Users\Rahulkumar\Desktop\python>python string_typecast.py
string value
1
12.6
True
False
65j

हालाँकि Output में ये values आपको normal numeric values लग रही होंगी अगर आप इनका type देखेंगे तो ये string ही हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers