20-对齐:ljust/rjust/center

20-对齐:ljust/rjust/center

语法讲解 今天学: ljust / rjust / center s = "hi" print(s.ljust(5, ".")) print(s.rjust(5

阅读更多 →
19-splitlines:keepends=True

19-splitlines:keepends=True

语法讲解 今天学: splitlines 的 keepends 参数 s = "a\nb\r\nc" parts = s.splitlines(keepends=True) pri

阅读更多 →
18-decode:errors='ignore'

18-decode:errors='ignore'

语法讲解 今天学: decode errors='ignore' b = b"hi\xff" print(b.decode("utf-8", e

阅读更多 →
17-编码:UTF-8 字节长度

17-编码:UTF-8 字节长度

语法讲解 今天学: encode 后按字节计数 print(len("猫".encode("utf-8"))) print(len("猫咪"

阅读更多 →
16-转义与原始字符串:长度陷阱

16-转义与原始字符串:长度陷阱

语法讲解 今天学: 转义与原始字符串 s1 = "a\nb" s2 = r"a\nb" print(s1) print(len(s2)) 小练习 运行下

阅读更多 →
15-format:补零与右对齐

15-format:补零与右对齐

语法讲解 今天学: format 格式控制 n = 7 print("{:03d}|{:>3d}".format(n, n)) 小练习 运行下面代码后输出是什么? n

阅读更多 →
14-isdigit/isdecimal:全角数字与罗马数字

14-isdigit/isdecimal:全角数字与罗马数字

语法讲解 今天学: 数字相关的字符串判定 a = "12" b = "Ⅻ" print(a.isdigit(), b.isdigit()) print(a.is

阅读更多 →
13-大小写:swapcase 与 title

13-大小写:swapcase 与 title

语法讲解 今天学: swapcase / title s = "Hello Python" print(s.swapcase()) print(s.title()) 小练习

阅读更多 →
12-translate:批量字符替换

12-translate:批量字符替换

语法讲解 今天学: translate 批量字符替换 table = str.maketrans({"e": "3", "o": "

阅读更多 →
11-replace:count 限制替换次数

11-replace:count 限制替换次数

语法讲解 今天学: replace 的 count 参数 s = "banana" print(s.replace("a", "A", 2)

阅读更多 →