日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python str / bytes / unicode 区别详解 - Python零基础入门教程

發(fā)布時間:2024/9/27 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python str / bytes / unicode 区别详解 - Python零基础入门教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 一.前言
  • 二.Python str / bytes / unicode 區(qū)別
    • 1.Python2.x 版本中 str / bytes / unicode 區(qū)別
    • 2.Python3.x 版本中 str / bytes / unicode 區(qū)別
  • 三.Python string 與 bytes 相互轉(zhuǎn)換
    • 1.string 經(jīng)過編碼 encode 轉(zhuǎn)化成 bytes
    • 2. bytes 經(jīng)過解碼 decode 轉(zhuǎn)化成 string
  • 四.猜你喜歡

零基礎(chǔ) Python 學(xué)習(xí)路線推薦 : Python 學(xué)習(xí)目錄 >> Python 基礎(chǔ)入門

一.前言

在講解 str / bytes / unicode 區(qū)別之前首先要明白字節(jié)和字符的區(qū)別,請參考:bytearray / bytes / string 區(qū)別 中對字節(jié)和字符有清晰的講解,最重要是明白:

  • 字符 str 是給人看的,例如:文本保存的內(nèi)容,用來操作的;
  • 字節(jié) bytes 是給計(jì)算機(jī)看的,例如:二進(jìn)制數(shù)據(jù),給計(jì)算機(jī)傳輸或者保存的;

二.Python str / bytes / unicode 區(qū)別

1.Python2.x 版本中 str / bytes / unicode 區(qū)別

在 Python2.x 版本中 str 跟 bytes 是等價的;值得注意的是:bytes 跟 unicode 是等價的,詳情見下圖

?

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python str / bytes / unicode 區(qū)別詳解.py @Time:2021/05/09 07:37 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累!"""s1 = u"Hello, World!" s2 = "Hello, World!" print(type(s1)) print(type(s2))''' 輸出:<type 'unicode'> <type 'str'> '''

2.Python3.x 版本中 str / bytes / unicode 區(qū)別

在 Python3.x 版本中 str 跟 unicode 是等價的;值得注意的是:bytes 跟 unicode 是不等價的,詳情見下圖

?

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python str / bytes / unicode 區(qū)別詳解.py @Time:2021/05/09 07:37 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累!"""s1 = u"Hello, World!" s2 = "Hello, World!" print(type(s1)) print(type(s2))''' 輸出:<class 'str'> <class 'str'> '''

三.Python string 與 bytes 相互轉(zhuǎn)換

1.string 經(jīng)過編碼 encode 轉(zhuǎn)化成 bytes

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python str / bytes / unicode 區(qū)別詳解.py @Time:2021/05/09 07:37 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累!"""s = "www.codersrc.com" #將字符串轉(zhuǎn)換為字節(jié)對象 b2 = bytes(s,encoding='utf8') #必須制定編碼格式 # print(b2)#方法一:字符串encode將獲得一個bytes對象 b3 = str.encode(s) #方法二:字符串encode將獲得一個bytes對象 b4 = s.encode() print(b3) print(type(b3)) print(b4) print(type(b4))''' 輸出結(jié)果:b'www.codersrc.com' <class 'bytes'> b'www.codersrc.com' <class 'bytes'> '''

2. bytes 經(jīng)過解碼 decode 轉(zhuǎn)化成 string

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python str / bytes / unicode 區(qū)別詳解.py @Time:2021/05/09 07:37 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累!"""# 字節(jié)對象b2# 如果含有中文,必須制定編碼格式,否則報錯TypeError: string argument without an encodingb2 = bytes("猿說python", encoding='utf8')# 方法二:bytes對象decode將獲得一個字符串s2 = bytes.decode(b2)# 方法二:bytes對象decode將獲得一個字符串s3 = b2.decode()print(s2)print(s3)''' 輸出結(jié)果:猿說python 猿說python '''

四.猜你喜歡

  • Python 條件推導(dǎo)式
  • Python 列表推導(dǎo)式
  • Python 字典推導(dǎo)式
  • Python 不定長參數(shù) *argc/**kargcs
  • Python 匿名函數(shù) lambda
  • Python return 邏輯判斷表達(dá)式
  • Python is 和 == 區(qū)別
  • Python 可變數(shù)據(jù)類型和不可變數(shù)據(jù)類型
  • Python 淺拷貝和深拷貝
  • Python 異常處理
  • Python 線程創(chuàng)建和傳參
  • Python 線程互斥鎖 Lock
  • Python 線程時間 Event
  • Python 線程條件變量 Condition
  • Python 線程定時器 Timer
  • Python 線程信號量 Semaphore
  • Python 線程障礙對象 Barrier
  • Python 線程隊(duì)列 Queue – FIFO
  • Python 線程隊(duì)列 LifoQueue – LIFO
  • Python 線程優(yōu)先隊(duì)列 PriorityQueue
  • Python 線程池 ThreadPoolExecutor(一)
  • Python 線程池 ThreadPoolExecutor(二)
  • Python 進(jìn)程 Process 模塊
  • Python 進(jìn)程 Process 與線程 threading 區(qū)別
  • Python 進(jìn)程間通信 Queue / Pipe
  • Python 進(jìn)程池 multiprocessing.Pool
  • Python GIL 鎖
  • 未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python str / bytes / unicode 區(qū)別詳解

    總結(jié)

    以上是生活随笔為你收集整理的Python str / bytes / unicode 区别详解 - Python零基础入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。