Python Casting

Casting in Python refers to converting one data type into another. Python provides several built-in functions for casting between types. This process is often called “type conversion” or “type casting.”

Types of Casting in Python

  1. Integer Casting (int())
  2. Float Casting (float())
  3. String Casting (str())

Each of these functions is used to convert between types, and Python will perform the conversion if possible.

1. Integer Casting (int())

The int() function is used to convert a number or a string that represents a number into an integer.

Example:

x = int(3.5)   # Converts float to int (result: 3)
y = int("10")  # Converts string to int (result: 10)
  • Note: When converting a floating-point number to an integer, the decimal part is truncated (not rounded).

Example:

a = 3.99
b = int(a)  # b becomes 3 (decimal part truncated)

c = "100"
d = int(c)  # d becomes 100 (string converted to integer)
  • If you try to convert a non-numeric string to an integer, you will get a ValueError.

Example (invalid conversion):

e = int("abc")  # Raises ValueError

2. Float Casting (float())

The float() function is used to convert an integer, a string, or a number into a floating-point number (decimal).

Example:

x = float(10)      # Converts int to float (result: 10.0)
y = float("3.14")  # Converts string to float (result: 3.14)

Example:

a = "25"
b = float(a)  # b becomes 25.0 (string to float)

c = 100
d = float(c)  # d becomes 100.0 (int to float)

3. String Casting (str())

The str() function converts integers, floats, or other types into strings.

Example:

x = str(10)        # Converts int to string (result: "10")
y = str(3.14)      # Converts float to string (result: "3.14")
z = str(True)      # Converts boolean to string (result: "True")

Example:

a = 123
b = str(a)  # b becomes "123" (int to string)

c = 45.67
d = str(c)  # d becomes "45.67" (float to string)

4. Other Types of Casting

  • List Casting (list()): Converts other iterable objects (e.g., strings, tuples) into lists.
    a = "hello"
    b = list(a)  # b becomes ['h', 'e', 'l', 'l', 'o'] (string to list)
    
    c = (1, 2, 3)
    d = list(c)  # d becomes [1, 2, 3] (tuple to list)
    
  • Tuple Casting (tuple()): Converts a list or another iterable into a tuple.
    a = [1, 2, 3]
    b = tuple(a)  # b becomes (1, 2, 3) (list to tuple)
    
  • Set Casting (set()): Converts a list, tuple, or string into a set (removes duplicates).
    a = [1, 2, 2, 3]
    b = set(a)  # b becomes {1, 2, 3} (list to set, duplicates removed)
    
  • Dictionary Casting (dict()): Converts a list of key-value pairs (e.g., a list of tuples) into a dictionary.
    a = [("name", "Alice"), ("age", 25)]
    b = dict(a)  # b becomes {'name': 'Alice', 'age': 25}
    

Type Conversion Examples:

# Integer to float
a = 10
b = float(a)  # b becomes 10.0

# Float to integer
c = 3.99
d = int(c)  # d becomes 3 (decimal part is truncated)

# String to integer
e = "100"
f = int(e)  # f becomes 100

# String to float
g = "3.14"
h = float(g)  # h becomes 3.14

# Integer to string
i = 123
j = str(i)  # j becomes "123"

# List to tuple
k = [1, 2, 3]
l = tuple(k)  # l becomes (1, 2, 3)

# String to list
m = "hello"
n = list(m)  # n becomes ['h', 'e', 'l', 'l', 'o']

Summary of Casting Functions:

FunctionDescriptionExample
int()Converts to integer (if possible)int("100")
float()Converts to float (if possible)float(3)
str()Converts to stringstr(3.14)
list()Converts to list (if possible)list("abc")
tuple()Converts to tuple (if possible)tuple([1, 2])
set()Converts to set (if possible)set([1, 2, 2])
dict()Converts to dictionary (if possible)dict([("a", 1)])
Leave a Reply 0

Your email address will not be published. Required fields are marked *