Python Casting


Specify a Variable Type

There may be times when you want to specify a type of variable. This can be done by broadcasting. Python is an object-oriented language, and as a result it uses classes to define data types, including its classics.

Therefore casting in python is done using the functions of a builder:

  • int() - create a whole number from a real number, float literal (by subtracting all decimals), or a literal character unit (giving the character unit represents the whole number)
  • float() - create a floating number from a real integer, a real float or a real character unit (as long as the character unit represents a float or a whole number)
  • str() - create a unit of characters in many types of data, including strings, whole numbers and floating words

Example

Integers

x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3


Example

Floats

x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2


Example

Strings

x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0# z will be '3.0'