Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1.2. Strings--String literals can span multiple lines 文档写法 探讨 #243

Closed
Delhpi opened this issue Mar 17, 2022 · 1 comment
Closed

Comments

@Delhpi
Copy link

@Delhpi Delhpi commented Mar 17, 2022

我这次没有写文档翻译错误,表示英文原文表达的意思和程序命令实际执行效果之间也有差异。
先看测试代码:
b="""
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
"""
print('b变量是默认输出:')
print(b)
for i in b:
if ord(i) == 10:
print('有换行符')

print('b变量的长度是',len(b))
print('b变量第一个字符的ASCii是',ord(b[0]))
print('b变量最后一个字符的ASCii是',ord(b[-1]))

a ="""
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
"""
print()
print('a变量是插入4个\字符后输出:')
print(a)
print('a变量的长度是',len(a))
print('a变量第一个字符的ASCii是',ord(a[0]), '字符:'+a[0])
print('a变量最后一个字符的ASCii是',ord(a[-1]), '字符:'+a[-1])

执行结果:
b变量是默认输出:

Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to

有换行符
有换行符
有换行符
有换行符
b变量的长度是 137
b变量第一个字符的ASCii是 10
b变量最后一个字符的ASCii是 10

a变量是插入4个\字符后输出:
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
a变量的长度是 133
a变量第一个字符的ASCii是 85 字符:U
a变量最后一个字符的ASCii是 111 字符:o

总结:
python默认给每个多行字符串里,增加了行数n+1个换行符,例子里是3行,所以是4个:其中开头一个,每行末尾一个。如果要取消加的4个换行符,那么在相应位置插入\字符。

再看原来的英文,怎么说明这个执行效果的:
End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line.
没有把程序执行的效果说完整。
首先,漏了增加的字符串开头换行符。
其实,后面半句说可以用\字符来prevent增加的换行符。这个是没有错,但是下面举的例子,其实不是在the end of the line插入\,恰恰是文档里没有说到的the start of the multiple lines。这样写文档和例子,读者会感觉到有点奇怪的。

@zhsj
Copy link
Collaborator

@zhsj zhsj commented Mar 17, 2022

英语原文的建议,请提交到 https://bugs.python.org/
步骤参考 https://docs.python.org/zh-cn/3/bugs.html

@zhsj zhsj closed this Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants