目录1️⃣ 第一行2️⃣ 第二行f-string3️⃣ !r 的作用4️⃣ repr() 和 str() 的区别5️⃣ 你的代码输出6️⃣ f-string 中的三个常见转换7️⃣ 常见使用场景调试这段 Python 代码主要演示f-string 中的!r转换标志的作用。我们逐步解释shello world!\nprint(f{s!r})1️⃣ 第一行shello world!\n字符串s的内容是hello world!但结尾有一个换行符\n。实际字符串内容是hello world!\n其中\n表示换行。2️⃣ 第二行f-stringprint(f{s!r})这里使用了f-string格式化字符串。基本格式f{expression}可以在{}中放变量或表达式。3️⃣!r的作用!r表示对表达式调用 repr()也就是repr(s)所以f{s!r}等价于repr(s)4️⃣repr()和str()的区别Python 有两种字符串表示方式函数用途str()给用户看的repr()给开发者看的更精确例子shello world!\nprint(s)输出hello world!因为\n变成了真实换行。如果print(repr(s))输出hello world!\n注意有引号\n被显示出来5️⃣ 你的代码输出shello world!\nprint(f{s!r})输出hello world!\n原因f{s!r} → repr(s)6️⃣ f-string 中的三个常见转换Python f-string 有三个常用转换写法等价{x}str(x){x!s}str(x){x!r}repr(x){x!a}ascii(x)例子shello\nprint(f{s})# hello (换行)print(f{s!s})# hello (换行)print(f{s!r})# hello\n7️⃣ 常见使用场景调试!r非常适合调试nameTom\nprint(fname {name!r})输出name Tom\n可以看到隐藏字符。✅一句话总结{s!r}等价于repr(s)作用是打印变量的“原始表示”包括引号和转义字符。