# 2021-01-11 #「Python」- 字符串操作(学习笔记)

格式化字符串

print("%s %s" % ("foo", "bar"))

求长度

len("foo")

常用转码操作

How to encode URLs in Python
How do I treat an ASCII string as unicode and unescape the escaped characters in it in python?

urllib.parse.quote(query) # 该函数不转码斜线
urllib.parse.quote_plus(query) # 转码空格到加号的所有字符

# Unicode decode
str.encode("utf-8").decode("unicode_escape")

两边去空白字符

Python Trim String – rstrip(), lstrip(), strip()

"".strip()
"".lstrip()
"".rstrip()

截取

str = "foo-bar"
str[0:4]

二进制 ⇔ 普通字符串

b'bin string'.decode('ascii')

参考文献

 

 


posted @ 2021-01-11 20:34  研究林纳斯写的  阅读(70)  评论(0编辑  收藏  举报