美文网首页python
python中 r'', b'', u'', f'' 的含义

python中 r'', b'', u'', f'' 的含义

作者: 菩提树下参悟 | 来源:发表于2023-05-07 12:45 被阅读0次
字符串前加 r

r'' 的作用是去除转义字符\
常见的转义符'\n' '\t' '\b'

string1 = r'Code \n YUN \t'
print(string1)
运行结果
字符串前加 f

以 f开头表示在字符串内支持大括号内的python 表达式

name = 'process'
string1 = f'Code  {name} (wenzi)'
print(string1)
运行结果
字符串前加 b
print("中文".encode(encoding="utf-8"))
print(b'\xe4\xb8\xad\xe6\x96\x87'.decode())
运行结果
字符串前加 u

后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。

string1 = u'我是含有中文字符组成的字符串。'
print(string1)
运行结果

相关文章

网友评论

    本文标题:python中 r'', b'', u'', f'' 的含义

    本文链接:https://www.haomeiwen.com/subject/zmyhsdtx.html