尝试在一行上打印多行字符串(字符串存储为变量)
Trying to print multiline string on one line (string stored as variable)
我正在学习麻省理工学院的计算和编程入门课程,我正在尝试将多行字符串存储在一个变量中,我可以使用该变量让程序与用户交互。
我知道 """
用回车符 return 插入换行符来输入长代码行(我想我的措辞有点准确)。
我 运行 的是存储的字符串在我的代码中看起来很糟糕,使用三重引号看起来更干净,但我仍然希望它打印在一行上。我正在尝试将其存储在这样的变量中:
inputRequest = """
Enter 'h' to indicate the guess is too high.
Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate the guess is correct.
"""
并且我尝试在我的控制台中调用该变量,如下所示:
print(inputRequest, end=" ")
但它仍然分三行打印出来。有没有一种有效的方法可以使我的代码看起来不乱?当我需要调用特定输出供用户交互时,将字符串存储在变量中似乎是减少输入的好方法,但我确信有更好的方法可以做到这一点。谢谢!
您可以在每行的末尾放置反斜杠,以防止在您的字符串中打印换行符。
inputRequest = """\
Enter 'h' to indicate the guess is too high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate the guess is correct. \
"""
print(inputRequest)
如果需要,您也可以使用单独的字符串来实现相同的目的。
inputRequest = \
"Enter 'h' to indicate the guess is too high. " \
"Enter 'l' to indicate the guess is too low. " \
"Enter 'c' to indicate the guess is correct. " \
print(inputRequest)
您的问题是该字符串包含固有的 EOL 字符。 print
语句不会 添加 任何换行符,但它们已经嵌入到您告诉它要打印的内容中。您需要替换那些,例如:
print(inputRequest.replace("\n", " ")
结果:
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate the guess is correct.
以下代码将帮助您实现您的目标:
print("Enter 'h' to indicate the guess is too high.",
"Enter 'l' to indicate the guess is too low.",
"Enter 'c' to indicate the guess is correct.")
或者您可以替换以下代码的 quotes.First 行说明了这一点:
print('Enter "h" to indicate the guess is too high.',
"Enter 'l' to indicate the guess is too low.",
"Enter 'c' to indicate the guess is correct.")
希望这就是您想要实现的目标并且对您有所帮助 ;)
干杯!
这里到处都是答案。这里有一个对您有用的实验。在 IDE 中输入以下行:
text = "This is string1. This is string2. This is string3"
现在在每个标点符号后按回车键手动格式化字符串,您将得到:
text = "This is string1." \
"This is string2." \
"This is string3."
以上是字符串连接,将以 "clean" 的方式提供您要查找的内容。接受的答案并不完全像 "clean" 那样,但因为是:"arguing semantics" XD
您可以在多行上创建单行字符串:
inputRequest = ("Enter 'h' to indicate the guess is too high. "
"Enter 'l' to indicate the guess is too low. "
"Enter 'c' to indicate the guess is correct.")
我正在学习麻省理工学院的计算和编程入门课程,我正在尝试将多行字符串存储在一个变量中,我可以使用该变量让程序与用户交互。
我知道 """
用回车符 return 插入换行符来输入长代码行(我想我的措辞有点准确)。
我 运行 的是存储的字符串在我的代码中看起来很糟糕,使用三重引号看起来更干净,但我仍然希望它打印在一行上。我正在尝试将其存储在这样的变量中:
inputRequest = """
Enter 'h' to indicate the guess is too high.
Enter 'l' to indicate the guess is too low.
Enter 'c' to indicate the guess is correct.
"""
并且我尝试在我的控制台中调用该变量,如下所示:
print(inputRequest, end=" ")
但它仍然分三行打印出来。有没有一种有效的方法可以使我的代码看起来不乱?当我需要调用特定输出供用户交互时,将字符串存储在变量中似乎是减少输入的好方法,但我确信有更好的方法可以做到这一点。谢谢!
您可以在每行的末尾放置反斜杠,以防止在您的字符串中打印换行符。
inputRequest = """\
Enter 'h' to indicate the guess is too high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate the guess is correct. \
"""
print(inputRequest)
如果需要,您也可以使用单独的字符串来实现相同的目的。
inputRequest = \
"Enter 'h' to indicate the guess is too high. " \
"Enter 'l' to indicate the guess is too low. " \
"Enter 'c' to indicate the guess is correct. " \
print(inputRequest)
您的问题是该字符串包含固有的 EOL 字符。 print
语句不会 添加 任何换行符,但它们已经嵌入到您告诉它要打印的内容中。您需要替换那些,例如:
print(inputRequest.replace("\n", " ")
结果:
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate the guess is correct.
以下代码将帮助您实现您的目标:
print("Enter 'h' to indicate the guess is too high.",
"Enter 'l' to indicate the guess is too low.",
"Enter 'c' to indicate the guess is correct.")
或者您可以替换以下代码的 quotes.First 行说明了这一点:
print('Enter "h" to indicate the guess is too high.',
"Enter 'l' to indicate the guess is too low.",
"Enter 'c' to indicate the guess is correct.")
希望这就是您想要实现的目标并且对您有所帮助 ;) 干杯!
这里到处都是答案。这里有一个对您有用的实验。在 IDE 中输入以下行:
text = "This is string1. This is string2. This is string3"
现在在每个标点符号后按回车键手动格式化字符串,您将得到:
text = "This is string1." \
"This is string2." \
"This is string3."
以上是字符串连接,将以 "clean" 的方式提供您要查找的内容。接受的答案并不完全像 "clean" 那样,但因为是:"arguing semantics" XD
您可以在多行上创建单行字符串:
inputRequest = ("Enter 'h' to indicate the guess is too high. "
"Enter 'l' to indicate the guess is too low. "
"Enter 'c' to indicate the guess is correct.")