字串以unicode編碼格式儲存
# 字串"u"字首&無字首案例
str1 =
'你好\thello'
str2 = u'你好\thello'
print
(str1)
print
(str2)
"""執行結果:
你好 hello
你好 hello
"""
字串所有字元視為普通字元
# 字串"r"字首案例
str1 = u'你好\thello'
str2 = r'你好\thello'
print
(str1)
print
(type
(str1)
)print
(str2)
print
(type
(str2)
)"""
執行結果:
你好 hello
你好\thello
"""
字串以ascii編碼格式儲存
# 字串"b"字首案例
str1 = b'你好\thello'
str2 = b'hello\thello'
print
(str1)
print
(str2)
"""執行結果:
syntaxerror: bytes can only contain ascii literal characters.
b'hello\thello'
"""
# str->bytes案例
s1 =
'hello,world'
print
(s1)
print
(type
(s1)
)s2 = s1.encode(
'utf-8'
)print
(s2)
print
(type
(s2)
)"""
執行結果:
hello,world
b'hello,world'
"""
# bytes->str案例
b1 = b'hello,world'
print
(b1)
print
(type
(b1)
)b2 = b1.decode(
'utf-8'
)print
(b2)
print
(type
(b2)
)"""
執行結果:
b'hello,world'
hello,world
"""
Python 字串字首
該部落格主要記錄下python字串的字首,讓後續的使用更加方便 後面字串以 unicode 格式 進行編碼,一般用在中文字串前面,防止因為原始碼儲存格式問題,導致再次使用時出現亂碼。作用 去掉反斜槓的轉移機制。特殊字元 即那些,反斜槓加上對應字母,表示對應的特殊含義的,比如最常見的 n 表示換行,t...
python字串前面加u,r,b的含義
u u 表示unicode字串 不是僅僅是針對中文,可以針對任何的字串,代表是對字串進行unicode編碼。一般英文本元在使用各種編碼下,基本都可以正常解析,所以一般不帶u 但是中文,必須表明所需編碼,否則一旦編碼轉換就會出現亂碼。建議所有編碼方式採用utf8 r r 非轉義的原始字串 與普通字元相...
python字串前面加u,r,b的含義
u u 表示unicode字串 不是僅僅是針對中文,可以針對任何的字串,代表是對字串進行unicode編碼。一般英文本元在使用各種編碼下,基本都可以正常解析,所以一般不帶u 但是中文,必須表明所需編碼,否則一旦編碼轉換就會出現亂碼。建議所有編碼方式採用utf8 r r 非轉義的原始字串 與普通字元相...