python怎么把字符串转换成数字

如题所述

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s1 = '123'
>>> s1
'123'
>>> i1 = int(s1)
>>> i1
123
>>> type(i1)
<class 'int'>
>>> s2 = '123.4'
>>> f2 = float(s2)
>>> f2
123.4
>>> type(f2)
<class 'float'>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-07
整数字符串转换为对应的整数
int('12')
小数字符串转换为对应小数
float('12.34')
数字转换为字符串
str(123.45)
ASCII码转换为相应字符
chr(97)
字符转换为响应ASCII码
ord('a')