[英]Formatting multi line strings in Python
使用这个伪代码:
1. 将数字声明为浮点数
2. 声明 Root 为 Float
3. 将响应声明为字符
4. 写Do you want to find the square root of a number?
5. Enter 'y' for yes, 'n' for no:
我编码的内容:
Response = str(input('Do you want to find the square root of a number? \
Enter y for yes, n for no: '))
PyCharm 运行窗口中的输出:
你想找到一个数的平方根吗? 输入 y 表示是,输入 n 表示否:
想要的格式结果:
Do you want to find the square root of a number?
Enter y for yes, n for no:
您可以使用\\n
打印换行符。
例子:
Response = input('Do you want to find the square root of a number?\nEnter y for yes, n for no: ')
输出:
Do you want to find the square root of a number?
Enter y for yes, n for no:
你快到了。 您只是在尝试打印的字符串中遗漏了一个 '\\n'。 这将以正确的格式打印您的字符串:
Response = str(input('Do you want to find the square root of a number?\nEnter y for yes, n for no: '))
你可以使用一个newline
作为所述的性格@MikeScotty
,或者你可以先用定义消息multi-line
string
。 这使得更改和创建消息比一遍又一遍地输入\\n
更容易。
为此,请使用triple-quoted
strings
:
m = """Do you want to find the square root of a number?
Enter y for yes, n for no: """
然后你可以简单地做:
input(m)
这将提示用户:
Do you want to find the square root of a number?
Enter y for yes, n for no: [their entry ready here]
当然,您不需要单独定义m
(消息),您可以直接将triple-quoted
string
写为input
的参数,但事情可能会开始变得有点混乱:
Response = input("""Do you want to find the square root of a number?
Enter y for yes, n for no: """)
您需要添加\\n
而不仅仅是\\
。 N 代表“新行”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.