Casting in Python
Casting in Python has quite a few practical applications. Here's what you need to know about casting in Python .
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:
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).Float()
- Construct a float from an integer, a float, or a string literal (provide the string representation of a float or an integer)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
You should read it
- What is Python? Why choose Python?
- 5 choose the best Python IDE for you
- Bookmark 5 best Python programming learning websites
- Multiple choice quiz about Python - Part 3
- For in Python loop
- Python data type: string, number, list, tuple, set and dictionary
- Why should you learn Python programming language?
- Multiple choice quiz about Python - Part 4
May be interested
- How to Check Python Version on Windows, Mac and Linuxto check which version of python is installed on your windows, mac or linux computer, all you have to do is simply run a single command.
- How to Start Programming in Pythonyou want to learn programming? the process of getting used to programming can make you feel discouraged and think that you need to go to school seriously to do it. with some languages, this is sometimes true. but there are also many programming languages that only take one to two days for you to grasp the basics of them. python [1] x research source is one such language. in just a few minutes, you can run a basic python program. read step 1 below to learn how.
- How to Uninstall Pythontoday's tipsmake will show you how to uninstall python applications and related files and folders on your computer. you can do it on both windows and mac computers.
- The Python Interpreter is now available on the Microsoft Storethe organization that operates and maintains python has decided to bring the python 3.11 interpreter and runtime to the microsoft store. this will greatly help users, creating convenience in downloading and using the python interpreter.
- How to install Python plugin to be able to execute Python programming on IntelliJis it possible to program python on intellij idea? instructions on setting up, installing python plugin to be able to program python on intellij ...
- The latest changes in Python 3.9python 3.9, released on october 5, brings significant changes to both the features of the language and how the language is developed.