If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
किसी दी गयी value /variable को दूसरे type के variable में convert करना Type Casting कहते हैं। different - different values को किसी particular type में convert करने के लिए Python में predefined constructors use किये जाते हैं।
किसी value को int / integer में convert करने के लिए int() का use किया जाता है। int() Boolean value True , False को 1 , 0 में convert करता है।
print( int(1) )
print( int(12.6) )
print( int('1') )
print( int("90") )
print( int(True) )
print( int(False) )
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'
value को float में convert करने के लिए float() constructor का use किया जाता है।
print( float(1) )
print( float(12.6) )
print( float('43.6') )
print( float(True) )
print( float(False) )
C:\Users\Rahulkumar\Desktop\python>python float_typecast.py 1.0 12.6 43.6 1.0 0.0
value को string में convert करने के लिए str() constructor का use किया जाता है।
print( str('string value') )
print( str(1) )
print( str(12.6) )
print( str(True) )
print( str(False) )
print( str(65j) )
C:\Users\Rahulkumar\Desktop\python>python string_typecast.py string value 1 12.6 True False 65j
हालाँकि Output में ये values आपको normal numeric values लग रही होंगी अगर आप इनका type देखेंगे तो ये string ही हैं।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook