码迷,mamicode.com
首页 > 编程语言 > 详细

python之bytes和string

时间:2017-04-10 09:45:41      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:内容   string   utf8   env   author   style   中间   abc   bin   

1、bytes主要是给在计算机看的,string主要是给人看的

2、中间有个桥梁就是编码规则,现在大趋势是utf8

3、bytes对象是二进制,很容易转换成16进制,例如\x64

4、string就是我们看到的内容,例如‘abc‘

5、string经过编码encode,转化成二进制对象,给计算机识别

6、bytes经过反编码decode,转化成string,让我们看,但是注意反编码的编码规则是有范围,\xc8就不是utf8识别的范围

7、实例:

#!/usr/bin/env python
# -*- coding: utf8 -*-
# __Author: "Skiler Hao"
# date: 2017/4/9 15:26
import hashlib

#字节对象b
b = b"example"
#字符串对象s
s = "example"
print(b)
print("example")

#将字符串转换为字节对象
b2 = bytes(s,encoding=utf8)  #必须制定编码格式
# print(b2)
#字符串encode将获得一个bytes对象
b3 = str.encode(s)
b4 = s.encode()
print(b3)
print(type(b3))
print(b4)
#将字节对象decode将获得一个str对象
s2 = bytes.decode(b)
s3 = b.decode()
print(s2)
print(s3)

 

python之bytes和string

标签:内容   string   utf8   env   author   style   中间   abc   bin   

原文地址:http://www.cnblogs.com/skiler/p/6687337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!