Casting in Python

Casting in Python has quite a few practical applications. Here's what you need to know about casting in Python .

Casting in Python Picture 1

Define variable type

Sometimes you may want to specify a type for a variable. This can be done by casting or casting in Python.

Python is an object-oriented language, so it uses classes to define data types, including its 'primitive' types.

Casting in Python is done using constructors:

  1. Int()- Constructs an integer from an integer, a float character (by removing all decimals) or a string literal (providing the string representation of an integer).
  2. Float()- Construct a float from an integer, a float, or a string literal (provide the string representation of a float or an integer)
  3. Str()- Build a string from a range of data types, including string, integer string, and float character string.

For example:

Integer:

x = int(1) # x sẽ là 1 y = int(2.8) # y sẽ là 2 z = int("3") # z sẽ là 3

Float:

x = str("s1") # x sẽ là 's1' y = str(2) # y sẽ là '2' z = str(3.0) # z sẽ là '3.0'

Table of data type casting functions in Python

STT Function and description
first int(x [,base]) casts to int
2 long(x [,base] ) casts to long int.
3 float(x) casts float.
4 complex(real [,imag]) casts complex number.
5 str(x) casts string.
6 repr(x) casts to the expression string.
7 eval(str) casts string to object.
8 tuple(s) casts tuple type.
9 list(s) casts the type list.
ten set(s) casts the set type.
11 dict(d) casts dictionary.
twelfth frozenset(s) casts the frozen set type.
13 chr(x) casts print to char
14 unichr(x) casts an int to a Unicode character.
15 ord(x) casts the character to int.
16 hex(x) converts integer to hexadecimal string.
17 oct(x) casts an integer to an octal string.

Above is the basic information you need to know about casting in Python . Hope the article is useful to you.

4 ★ | 1 Vote

May be interested

  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • Multiple choice quiz about Python - Part 4Multiple choice quiz about Python - Part 4
    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
  • How to use Closure in PythonHow to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Functions in PythonFunctions in Python
    what is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
  • How to use GPT-3 with PythonHow to use GPT-3 with Python
    feel free to use cool gpt-3 technology with your own python scripts using openai's handy api. here are details on how to use gpt-3 with python.
  • Multiple choice quiz about Python - Part 7Multiple choice quiz about Python - Part 7
    following the previous test set, part 7 continues with the topic built-in functions in python. let's try with quantrimang to try the 10 questions below.
  • The next () function in PythonThe next () function in Python
    the next () function in python returns the next element in the iterator. you can add a default value to return if iterable is already the last element.
  • Multiple choice quiz about Python - Part 10Multiple choice quiz about Python - Part 10
    following the previous test, part 10 returned with the python function. let's try with quantrimang to try the 10 questions below.
  • Why should you learn Python programming language?Why should you learn Python programming language?
    python is a multi-purpose programming language created in the late 1980s and named after monty python drama group. let's tipsmake.com find out 3 reasons you should learn python programming language in this article!
  • Package in PythonPackage in Python
    we will learn how to divide code into efficient, clear modules, using python packages. plus, the way to import and use your own package, or the package you download from somewhere into the python program.