Discuss / Python / 实验结果和文中不一致

实验结果和文中不一致

Topic source

Cao Yi

#1 Created at ... [Delete] [Delete and Lock User]

Windows Terminal 中启动 Python 控制台,输出和文中不一致,如下:

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'ABC\\-001'
>>> s
'ABC\\-001'
>>> s = r'ABC\-001'
>>> s
'ABC\\-001'
>>>

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

注意print和单敲一个s不一样:

>>> print(s)
ABC\-001
>>> s
'ABC\\-001'

print才是s的内容,单敲s是python的字符串格式

Cao Yi

#3 Created at ... [Delete] [Delete and Lock User]

谢谢廖老师!


  • 1

Reply