普通字符串

一般字符串都是已unicode编码,且和C类似,可以使用\来转义,比如

a = "test\ntest"
print(a)

输出

test
test

前面加r

在字符串前面加上一个 r 表示该字符串为raw string,不识别转义。

b = r"test\ntest"
print(b)

输出

test\ntest

这在使用正则表达式的时候很有用。

前面加b

生成字节序列对象bytearray。这在需要按字节序列发送数据时有用,比如网络发送

message = b"GET / HTTP/1.1\r\n\r\n"
s.sendall(message)

message的类型不再是str,而是bytes了。

<class 'bytes'>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案