代码总结Python2 和 Python3 字符串的区别
2020-02-13 11:30
691 查看
Python2
>>> >>> isinstance(b'abc', bytes) True >>> >>> isinstance(b'abc', str) True >>> >>> isinstance('abc', str) True >>> >>> isinstance('abc', bytes) True >>> >>> >>> >>> 'abc'.startswith('ab') True >>> >>> b'abc'.startswith('ab'.encode()) True >>> >>> b'abc'.startswith('ab') True >>> >>> 'abc'.startswith('ab'.encode()) True >>>
Python3
>>> >>> isinstance(b'abc', bytes) True >>> >>> isinstance(b'abc', str) False >>> >>> isinstance('abc', str) True >>> >>> isinstance('abc', bytes) False >>> >>> >>> >>> 'abc'.startswith('ab') True >>> >>> b'abc'.startswith('ab'.encode()) True >>> >>> b'abc'.startswith('ab') Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> b'abc'.startswith('ab') TypeError: startswith first arg must be bytes or a tuple of bytes, not str >>> >>> 'abc'.startswith('ab'.encode()) Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> 'abc'.startswith('ab'.encode()) TypeError: startswith first arg must be str or a tuple of str, not bytes >>>
扩展学习
python2中有一种类型叫做unicode型,例
type(u"a") => str型 type("a".decode('utf8')) => unicode型
两者返回的类型都是unicode型
而在python3中,所有的字符串都是unicode,所以就不存在单独的unicode型,全部都是字符串型
type(u"a") => str型 type("a".decode('utf8')) => 报错,python3不能这样写
但是python3中多处一种字符串
type(b'132') => byte型
以上就是相关的知识点内容,如果大家有任何补充可以联系脚本之家小编。
您可能感兴趣的文章:
- python版本坑:md5例子(python2与python3中md5区别)
- Python3.x和Python2.x的区别介绍
- python3 与python2 异常处理的区别与联系
- python2 与python3的print区别小结
- Python2.x与Python3.x的区别
- 浅谈Python2.6和Python3.0中八进制数字表示的区别
- 浅述python2与python3的简单区别
- Python2与Python3的区别实例总结
- Python2与Python3的区别实例分析
- Python2.X/Python3.X中urllib库区别讲解
- python2 中 unicode 和 str 之间的转换及与python3 str 的区别
- python2和python3的输入和输出区别介绍
- python2和python3在处理字符串上的区别详解
- python面试题Python2.x和Python3.x的区别
- python3 assert 断言的使用详解 (区别于python2)
- Python2与Python3的区别点整理
- Python2与Python3的区别详解
相关文章推荐
- Python字符串使用总结
- python和actionscript的代码组织方式区别
- python 常用小总结(主要字符串)
- python 2.4与2.5中字符串与datetime转换的区别
- Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数
- python使用正则搜索字符串或文件中的浮点数代码实例
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- python字符串符号区别
- python 字符串常用方法总结
- python字符串使用方法总结
- Python中字符串的总结
- Python练习代码 -- 字符串和正则表达式, 文件文件夹操作
- [python01] python列表,元组对比Erlang的区别总结
- Python 3.2 字符串方法学习总结
- Python3.2中的字符串函数学习总结
- Python字符串拼接、截取及替换方法总结
- Python字符串加密解密方法总结
- Python raw_input和input总结 在版本2和版本3中的区别
- python 3.2 字符串方法学习总结
- python字符串处理函数总结