如何编码和打印带有换行符格式的字符串?

How to code and print a character string formatted with line breaks?

如果我有一个包含换行符的字符串,我如何在不手动在行之间添加 \n 的情况下在 R 中对其进行编码,然后用换行符打印它?应该有多行输出;原始字符串的每一行应打印为单独的一行。

这是如何完成 Python 中的任务的示例:

string = """
  Because I could
  Not stop for Death
  He gladly stopped for me
  """

示例用例:我有一段很长的 SQL 代码,其中包含一堆换行符和子命令。我想将代码作为单个字符串输入以供稍后评估,但手动清理它会很困难。

不需要什么特别的。只是开头和结尾的引号。

在 R 中:

x = "Because I could
Not stop for Death
He gladly stopped for me"
x
# [1] "Because I could\nNot stop for Death\nHe gladly stopped for me"

cat(x)
# Because I could
# Not stop for Death
# He gladly stopped for me

在Python中:

>>> string = """
...     Because I could
...     Not stop for Death
...     He gladly stopped for me
... """
>>> string
'\n\tBecause I could\n\tNot stop for Death\n\tHe gladly stopped for me\n'