新手求救,佬佬求求

Python
呜呜呜,大佬求求,这是怎么了呀,不管\的实际含义和作用是什么,为什么明明removeprefix的()里和url里的\都是一样的,但就是没办法消除呢,:sob: :sob: :sob:

Jason990420
最佳答案
D:\>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> url = "http:\\python.com"
>>> print(f"{url.removeprefix('http:\\')}")
  File "<stdin>", line 1
    print(f"{url.removeprefix('http:\\')}")
                                          ^
SyntaxError: f-string expression part cannot include a backslash

D:\>python
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> url = "http:\\python.com"
>>> print(f"{url.removeprefix('http:\\')}")
python.com


Python旧版 <=3.11, f-string 实现施加了一些限制:

  • 无法重复使用引号或字符串分隔符
  • 无法嵌入反斜杠,这意味着不能使用转义字符
  • 禁止添加内联注释
  • f-string 的嵌套仅限于 Python 中可用的引用变体


请使用 Python 3.12+

Python 3.12 有什么新功能

Python 3.12 语言变更主要集中在可用性,因为 f-字串已经移除了许多限制.


PEP 701: Syntactic formalization of f-strings

PEP 701 lifts some restrictions on the usage of f-strings. Expression components inside f-strings can now be any valid Python expression, including strings reusing the same quote as the containing f-string, multi-line expressions, comments, backslashes, and unicode escape sequences.

8小时前 评论
FenLi (楼主) 7小时前
Jason990420 (作者) 7小时前
讨论数量: 9

函数写错了printf

11小时前 评论
FenLi (楼主) 11小时前

url = "http://python.com" print(url.removeprefix('http://'))

11小时前 评论
FenLi (楼主) 7小时前
Jason990420
D:\>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> url = "http:\\python.com"
>>> print(f"{url.removeprefix('http:\\')}")
  File "<stdin>", line 1
    print(f"{url.removeprefix('http:\\')}")
                                          ^
SyntaxError: f-string expression part cannot include a backslash

D:\>python
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> url = "http:\\python.com"
>>> print(f"{url.removeprefix('http:\\')}")
python.com


Python旧版 <=3.11, f-string 实现施加了一些限制:

  • 无法重复使用引号或字符串分隔符
  • 无法嵌入反斜杠,这意味着不能使用转义字符
  • 禁止添加内联注释
  • f-string 的嵌套仅限于 Python 中可用的引用变体


请使用 Python 3.12+

Python 3.12 有什么新功能

Python 3.12 语言变更主要集中在可用性,因为 f-字串已经移除了许多限制.


PEP 701: Syntactic formalization of f-strings

PEP 701 lifts some restrictions on the usage of f-strings. Expression components inside f-strings can now be any valid Python expression, including strings reusing the same quote as the containing f-string, multi-line expressions, comments, backslashes, and unicode escape sequences.

8小时前 评论
FenLi (楼主) 7小时前
Jason990420 (作者) 7小时前
Jason990420

在 Python 旧版中

反斜线是 Python 字串中的转义字符,用于表示特殊字符,例如换行符 ( \n)、制表符 ( \t) 或转义引号。 允许反斜线直接出现在 f 字串的表达式部分会为 f 字串解析器带来极大的复杂性。 解析器需要区分表达式字串字面量中的反斜杠和 f 字串本身的转义序列中的反斜杠,这可能会导致歧义并给解析带来挑战。

设计者的目标是让 f 字串在常见用例中保持简洁直觉。 在表达式部分允许使用复杂的转义序列可能会使 f 字串更难阅读和理解,尤其是对于初学者而言。

7小时前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!