Dinesh Beniwal
What is type conversion in Python?
By Dinesh Beniwal in Python on Jun 20 2019
  • Komal Sharma
    Jun, 2019 23

    Python has type conversion function which directly convert one datatype to another. like: float a=2; int b = int(a);

    • 1
  • Laxmidhar Sahoo
    Jan, 2020 22

    1. Type Conversion is the conversion of object from one data type to another data type.
    2. Implicit Type Conversion is automatically performed by the Python interpreter.
    3. Python avoids the loss of data in Implicit Type Conversion.
    4. Explicit Type Conversion is also called Type Casting, the data types of object are converted using predefined function by user.
    5. In Type Casting loss of data may occur as we enforce the object to specific data type.
    6. Syntax :
    7. (required_datatype)(expression)
    8. # initializing string
    9. s = "10010"
    10. # printing string converting to int base 2
    11. c = int(s,2)
    12. print (c)
    13. # printing string converting to float
    14. e = float(s)
    15. s = '4'
    16. # printing character converting to integer
    17. c = ord(s)
    18. print (c)
    19. # printing integer converting to hexadecimal string
    20. c = hex(56)
    21. print (c)
    22. # printing integer converting to octal string
    23. c = oct(56)
    24. print (c)
    25. # initializing string
    26. s = 'geeks'
    27. # printing string converting to tuple
    28. c = tuple(s)
    29. print (c)
    30. # printing string converting to set
    31. c = set(s)
    32. print (c)
    33. # printing string converting to list
    34. c = list(s)
    35. print ("After converting string to list : ",end="")
    36. print (c)

    • 0

Most Popular Job Functions


MOST LIKED QUESTIONS