close

undefined

Python 3 中的編碼預設就是unicode,不過在字串前加上u,依舊可以強迫轉換後續字串為unicode:

#將字串編碼成 unicode
str1 = u"今天天氣很好"
print(str1)

 

結果:

今天天氣很好

 

字串前面加上r代表不將特殊字元編譯:

#將字串中的特殊符號不進行編譯
str2 = r"我\n想吃水果"
str3 = "我\n想吃水果"
print(str2)
print(str3)

 

結果:

\n想吃水果

想吃水果

很清楚地發現str3中的\n有被編譯成換行

 

最後一個b代表bytespython 2中的字串預設編碼是 bytes,它只能對應到python 3 ASCII 中的編碼,如果字串是中文字就會報錯:

#將字串編碼成bytes
str4 = b"Eat an apple"
print(str4)

 

結果:

b'Eat an apple'

 

[Reference]

1. https://www.itread01.com/p/519776.html

 

arrow
arrow
    全站熱搜

    葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()