0

0

如何在Python中将字符串转换为数字?

王林

王林

发布时间:2023-08-26 19:37:03

|

5054人浏览过

|

来源于tutorialspoint

转载

如何在python中将字符串转换为数字?

要将字符串转换为数字,有多种方法。让我们一一看看。

使用 int() 将字符串转换为数字

示例

在此示例中,我们将使用 int() 方法将字符串转换为数字 -

# String to be converted
myStr = "200"

# Display the string and it's type
print("String = ",myStr)
print("Type= ", type(myStr))

# Convert the string to integer using int() and display the type
myInt = int(myStr)
print("\nInteger = ", myInt)
print("Type = ", type(myInt))

输出

String =  200
Type=  

Integer =  200
Type =  

使用 float() 将字符串转换为数字

示例

在此示例中,我们将使用 float() 方法将字符串转换为浮点数,然后使用 int() 方法将浮点数转换为整数 -

JSON.NET 简单的使用 中文WORD版
JSON.NET 简单的使用 中文WORD版

本文档主要讲述的是JSON.NET 简单的使用;JSON.NET使用来将.NET中的对象转换为JSON字符串(序列化),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?)。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

下载
# String to be converted
myStr = "500"

# Display the string and it's type
print("String = ",myStr)
print("Type= ", type(myStr))

# Convert the string to float
myFloat = float(myStr)
print("\nFloat = ", myFloat)
print("Type = ", type(myFloat))

# Convert the float to int
myInt = int(myFloat)
print("\nInteger = ", myInt)
print("Type = ", type(myInt))

输出

String =  500
Type=  

Float =  500.0
Type =  

Integer =  500
Type =  

将字符串转换为数字base10和base8

示例

在此示例中,我们将使用带有基本参数的 int() 将字符串转换为数字。

立即学习Python免费学习笔记(深入)”;

# String to be converted
myStr = "500"

# Display the string and it's type
print("String = ",myStr)
print("Type= ", type(myStr))

# Convert the string to int
myInt1 = int(myStr)
print("\nInteger (base10) = ", myInt1)
print("Type = ", type(myInt1))

# Convert the string to int
myInt2 = int(myStr, base=8)
print("\nInteger (base8) = ", myInt2)
print("Type = ", type(myInt2))

输出

String =  500
Type=  
Integer (base10) =  500
Type =  

Integer (base8) =  320
Type =  

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
vlookup函数使用大全
vlookup函数使用大全

本专题整合了vlookup函数相关 教程,阅读专题下面的文章了解更多详细内容。

26

2025.12.30

金山文档相关教程
金山文档相关教程

本专题整合了金山文档相关教程,阅读专题下面的文章了解更多详细操作。

28

2025.12.30

PS反选快捷键
PS反选快捷键

本专题整合了ps反选快捷键介绍,阅读下面的文章找到答案。

25

2025.12.30

表格中一行两行的方法
表格中一行两行的方法

本专题整合了表格中一行两行的相关教程,阅读专题下面的文章了解更多详细内容。

3

2025.12.30

cpu温度过高解决方法大全
cpu温度过高解决方法大全

本专题整合了cpu温度过高相关教程,阅读专题下面的文章了解更多详细内容。

5

2025.12.30

ASCII码介绍
ASCII码介绍

本专题整合了ASCII码相关内容,阅读专题下面的文章了解更多详细内容。

31

2025.12.30

GPS是什么
GPS是什么

本专题整合了GPS相关内容,阅读专题下面的文章了解更多详细内容。

3

2025.12.30

wifi拒绝接入
wifi拒绝接入

本专题整合了wifi拒绝接入相关教程,阅读下面的文章了解更多详细方法。

9

2025.12.30

丰网速运介绍
丰网速运介绍

本专题整合了丰网速运查询入口以及相关内容,阅读专题下面的文章了解更多内容。

3

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

Swoft2.x速学之http api篇课程
Swoft2.x速学之http api篇课程

共16课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号