显式转换

int(x [,base]) ⇒ 将x转换为一个十进制的整数

long(x [,base]) ⇒ 将x转换为一个十进制的长整数

float(x) ⇒ 将x转换为一个浮点数

str(object) ⇒ 转换为字符串

repr(object) ⇒ 转换为表达式字符串

eval(str) ⇒ 用来计算在字符串中的有效Python表达式,并返回一个对象

tuple(seq) ⇒ 将序列seq转换为一个元组

list(seq) ⇒ 将序列seq转换为一个列表

chr(x ) ⇒ 将一个整数转换为一个字符

unichr(x ) ⇒ 将一个整数转换为Unicode字符

ord(x ) ⇒ 将一个字符转换为它的整数值

hex(x ) ⇒ 将一个整数转换为一个十六进制字符串

oct(x ) ⇒ 将一个整数转换为一个八进制字符串

Non-String转换成String类型

1.List -> String

>>> li = ['My','name','is','Jmilk']
>>> strLi = str(li)
>>> print(strLi)
['My', 'name', 'is', 'Jmilk']
>>> type(strLi)
<class 'str'>

2.Tuple -> String

>>> tup = ('my','name','is','jmilk')
>>> Tup = str(tup)
>>> Tup
"('my', 'name', 'is', 'jmilk')"
>>> type(Tup)
<class 'str'>

3.Dictionary -> String

>>> dic = {'name':'Jmilk','age':23}
>>> Dic = str(dic)
>>> Dic
"{'name': 'Jmilk', 'age': 23}"
>>> type(Dic)
<class 'str'>

str()和repr()的区别

(1)repr()支持eval(repr(object)) == object。

(2)str()函数的结果适合用户阅读,repr()的结果适合python解析器阅读,其中有一些Python解析器能够识别的数据细节,但这些细节一般对用户是多余的。

(3)repr()转换后的String对象可以通过求值运算eval()来还原到转换之前的对象,str()通常不需要eval()去处理。

>>> name = ('alex\n,reic\n,jane\n')
>>> print(str(name))
alex
,reic
,jane >>> print(repr(name))
'alex\n,reic\n,jane\n'

eval():

(1)能结合repr()函数将一个经过转换为Strng类型后的对象还原为转换之前的对象类型。

(2)eval()也被称为求值运算,可以将字符串str当成有效的表达式来求值并返回计算结果。

>>> name = ('My name is Jmilk\n')
>>> name1 = str(name)
>>> name1
'My name is Jmilk\n'
>>> name2 = repr(name)
>>> name2
"'My name is Jmilk\\n'"
>>> eval(name1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
My name is Jmilk
^
SyntaxError: invalid syntax
>>> eval(name2)
'My name is Jmilk\n'

eval(str(Object))可能会报错,无法还原为原来的对象型。而eval(repr(object))却可以还原,下面例子:

>>> name = ('My','name','is','Jmilk\n')

>>> repr(name)
"('My', 'name', 'is', 'Jmilk\\n')" >>> eval(repr(name))
('My', 'name', 'is', 'Jmilk\n') >>> type(eval(repr(name)))
<class 'tuple'>

总结:str()函数主要是为了让人能够更好的阅读其内容,而rper()除了转换为String类型外,还能够被Python解析器识别其数据细节,从而repr()转换后的字符串能够被当作有效表达式来处理。
       注意:eval()函数最主要的功能是能够将字符串中有效的表达式进行计算并返回一个对象。如下:

>>> sum = '100 + 10'
>>> eval(sum)
110

Non-int转换成int类型 

int(x[, base=10]) -> int or long                         base:指定进制;  x:通常为一个String  ;base指定了x的进制。

1.float -> int  向下取整

>>> int(10.9)
10

2.string ->int  int('a',b) a为要转换的数字,b为a的进制,b必须标明,不然会出错。

>>> int('0xa',16)
10
>>> int('',2)
10
>>> int('0xa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0xa'

 Non-float转换成float

float(x) -> floating point number
       Convert a string or number to a floating point number, if possible.
       可以接收Int和String类型参数,float()函数在连接数据库操作会被经常使用。当参数为String时,只能出现数字和一个点额任意组合,若出现多个点号,则会出现异常。

>>> float(10)
10.0 >>> float('')
100.0 >>> float('.98')
0.98 >>> float('.123.')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '.123.'

Non-list转换成list

list(iterable) -> new list initialized from iterable’s items 
       使用迭代器中的元素生成一个新的列表

1.string -> list

Python全栈之路----类型转换的更多相关文章

  1. Python全栈之路----目录

    Module1 Python基本语法 Python全栈之路----编程基本情况介绍 Python全栈之路----常用数据类型--集合 Module2 数据类型.字符编码.文件操作 Python全栈之路 ...

  2. Python全栈之路目录结构

    基础 1.Python全栈之路-----基础篇 2.Python全栈之路---运算符与基本的数据结构 3.Python全栈之路3--set集合--三元运算--深浅拷贝--初识函数 4.Python全栈 ...

  3. Python全栈之路----常用模块----hashlib加密模块

    加密算法介绍 HASH       Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...

  4. python 全栈之路

    目录 Python 全栈之路 一. Python 1. Python基础知识部分 2. Python -函数 3. Python - 模块 4. Python - 面对对象 5. Python - 文 ...

  5. Python全栈之路----函数----返回值

    函数外部的代码想要获取函数的执行结果,就可以在函数里用return语句,把结果返回. def stu_register(name,age,course='PY',country='CN'): prin ...

  6. Python全栈之路----常用模块----软件开发目录规范

    目录基本内容 log  #日志目录 conf  #配置目录 core/luffycity  #程序核心代码目录  #luffycity 是项目名,建议用小写 libs/modules  #内置模块 d ...

  7. Python全栈之路----常用模块----shutil模块

    高级的 文件.文件包.压缩包 处理模块   参考Python之路[第四篇]:模块     #src是原文件名,fdst是新文件名 shutil.copyfileobj(fsrc, fdst[, len ...

  8. Python全栈之路----Python2与Python3

    金角大王Alex  python 之路,致那些年,我们依然没搞明白的编码 python2与python3的区别 py2 str = bytes 为什么有bytes? 是因为要表示图片.视频等二进制格式 ...

  9. Python全栈之路----函数进阶----装饰器

    Python之路,Day4 - Python基础4 (new版) 装饰器 user_status = False #用户登录后改为True def login(func): #传入想调用的函数名 de ...

随机推荐

  1. gitlab 迁移

    http://www.cnblogs.com/crysmile/p/9505527.html

  2. read_csv 的 names 和 index_col 参数作用

  3. Linux上 发布.Net Core

    环境准备 下面我们使用VM虚拟机.我这里安装的Linux系统是centos7 软件提供: VM: https://www.vmware.com/cn.html centos7 Minimal :htt ...

  4. Kotlin 条件控制

    IF 表达式 一个 if 语句包含一个布尔表达式和一条或多条语句. // 传统用法 var max = a if (a < b) max = b // 使用 else var max: Int ...

  5. spark streaming 整合 kafka(一)

    转载:https://www.iteblog.com/archives/1322.html Apache Kafka是一个分布式的消息发布-订阅系统.可以说,任何实时大数据处理工具缺少与Kafka整合 ...

  6. R apply函数 三维 array

    参考自:https://www.cnblogs.com/nanhao/p/6674063.html 首先,生成三维数组,注意该三维矩阵为 2*3*4的维度: x=array(1:24,c(2,3,4) ...

  7. 在Altium Designer 10中实现元器件旋转45°角放置

    Tool--->Preferences >> PCB Editor >> General 将Rotation Step(旋转的步进值)由90改为45,这样以后每次按空格键 ...

  8. 简单的class及运算符重载

    #include <iostream> #include <vector> #include <algorithm> using namespace std; cl ...

  9. Token国内地铁使用城市

    天津 广州 深圳 南京 武汉 台北 高雄

  10. iis7.0 win7如何修改默认iis端口号

    iis7与iis6的设置方法要详细很多.所以,在更改设置上,iis7反而显得更复杂.iis作为本地网页编辑环境,占用80端口都是理所当然的.但是,作为网页调试的技术人员,通常本地都会安装iis.Apa ...