11-replace:count 限制替换次数

语法讲解 今天学: replace 的 count 参数 s = "banana" print(s.replace("a", "A", 2)) 小练习 运行下面代码后输出是

开始练习 →

12-translate:批量字符替换

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

开始练习 →

13-大小写:swapcase 与 title

语法讲解 今天学: swapcase / title s = "Hello Python" print(s.swapcase()) print(s.title()) 小练习 运行下面代码后输出是什么?(多行输出请

开始练习 →

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

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

开始练习 →

15-format:补零与右对齐

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

开始练习 →

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

语法讲解 今天学: 转义与原始字符串 s1 = "a\nb" s2 = r"a\nb" print(s1) print(len(s2)) 小练习 运行下面代码后输出是什么?(多行输出请按真实换

开始练习 →

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

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

开始练习 →

18-decode:errors='ignore'

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

开始练习 →

19-splitlines:keepends=True

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

开始练习 →

20-对齐:ljust/rjust/center

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

开始练习 →