Summary of python type conversion methods
How to type conversion in python?
Related recommendations: "python video"
int
Supports conversion to int type, only float, str, bytes, other types None are supported.
float -> int
will remove the decimal point and the following values, leaving only the integer part.
1 |
|
str -> int
If there are characters other than numbers (0-9) and signs (/-) in the string, an error will be reported .
1 2 3 |
|
bytes -> int
If bytes contains characters other than numbers (0-9) and signs (/-), an error will be reported.
1 2 3 |
|
float
Supports conversion to float type, only int, str, bytes, other types are not supported.
int -> float
When converting int to float, one decimal place will be added automatically.
1 |
|
str -> float
If the string contains characters other than sign (/-), numbers (0-9) and decimal point (.) , conversion is not supported.
1 2 |
|
bytes -> float
If bytes contains characters other than signs (/-), numbers (0-9) and decimal points (.) , conversion is not supported.
1 2 |
|
complex
Only supports int, float, str conversion to complex type.
int -> complex
int When converting complex, the imaginary part will be automatically added and represented by 0j.
1 |
|
float -> complex
When converting float, the imaginary part will be automatically added and represented by 0j.
1 |
|
str -> complex
str When converting complex, if it can be converted to int or float, it will be converted and then converted to complex. If the string completely conforms to the complex expression rules, it can also be converted to a complex type value.
1 2 3 4 5 6 7 8 9 |
|
str
str() function can convert any object into a string.
int -> str
int Converting str will directly convert it completely.
1 |
|
float -> str
float Converting str will remove the decimal part with the last 0.
1 |
|
complex -> str
complex conversion to str will first convert the value into a standard complex expression, and then convert it into a string.
1 2 |
|
bytes -> str
The conversion between bytes and str is special. In Python 3.x, strings and bytes are no longer confused, but are completely different data types.
Convert to an executable expression string:
1 |
|
The str() function specifies the encoding parameter, or you can use the bytes.decode() method to convert actual data:
1 2 3 |
|
list -> str
会先将值格式化为标准的 list 表达式,然后再转换为字符串。
1 2 3 |
|
tuple -> str
会先将值格式化为标准的 tuple 表达式,然后再转换为字符串。
1 2 3 |
|
dict -> str
会先将值格式化为标准的 dict 表达式,然后再转换为字符串。
1 2 3 |
|
set -> str
会先将值格式化为标准的 set 表达式,然后再转换为字符串。
1 2 3 |
|
其他类型
转换内置对象:
1 2 |
|
转换类实例:
1 2 3 4 5 |
|
转换函数:
1 2 3 4 |
|
bytes
仅支持 str 转换为 bytes 类型。
1 2 3 |
|
list
支持转换为 list 的类型,只能是序列,比如:str、tuple、dict、set等。
str -> list
1 2 |
|
bytes -> list
bytes 转换列表,会取每个字节的 ASCII 十进制值并组合成列表
1 2 |
|
tuple -> list
tuple 转换为 list 比较简单。
1 2 |
|
dict -> list
字典转换列表,会取键名作为列表的值。
1 2 |
|
set -> list
集合转换列表,会先去重为标准的集合数值,然后再转换。
1 2 |
|
tuple
与列表一样,支持转换为 tuple 的类型,只能是序列。
str -> tuple
1 2 |
|
bytes -> tuple
bytes 转换元组,会取每个字节的 ASCII 十进制值并组合成列表。
1 2 |
|
list -> tuple
1 2 |
|
dict -> tuple
1 2 |
|
set -> tuple
1 2 |
|
dict
str -> dict
使用 json 模块
使用 json 模块转换 JSON 字符串为字典时,需要求完全符合 JSON 规范,尤其注意键和值只能由单引号包裹,否则会报错。
1 2 3 4 |
|
使用 eval 函数
因为 eval 函数能执行任何符合语法的表达式字符串,所以存在严重的安全问题,不建议。
1 2 3 |
|
使用 ast.literal_eval 方法
使用 ast.literal_eval 进行转换既不存在使用 json 进行转换的问题,也不存在使用 eval 进行转换的 安全性问题,因此推荐使用 ast.literal_eval。
1 2 3 4 5 |
|
list -> dict
通过 zip 将 2 个列表映射为字典:
1 2 3 4 |
|
将嵌套的列表转换为字典:
1 2 3 4 5 6 7 |
|
tuple -> dict
通过 zip 将 2 个元组映射为字典:
1 2 3 4 |
|
将嵌套的元组转换为字典:
1 2 3 4 5 6 7 |
|
set -> dict
通过 zip 将 2 个集合映射为字典:
1 2 3 4 |
|
set
str -> set
先将字符切割成元组,然后再去重转换为集合。
1 |
|
bytes -> set
会取每个字节的 ASCII 十进制值并组合成元组,再去重。
1 |
|
list -> set
先对列表去重,再转换。
1 |
|
tuple -> set
先对列表去重,再转换。
1 |
|
dict -> set
会取字典的键名组合成集合。
1 2 |
|
The above is the detailed content of Summary of python type conversion methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.
