Python 打印与 Javascript console.log()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31260437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:26:42  来源:igfitidea点击:

Python print vs Javascript console.log()

javascriptconsole.log

提问by shintocv

In Python:

在 Python 中:

print [1,2], '\n', [3,4]

would print

会打印

[1,2]
[3,4]

In Javascript:

在 JavaScript 中:

console.log([1,2],'\n',[3,4])

prints

印刷

[1,2] '\n' [3,4]

What is the equivalent Javascript statement to the above Python print?

与上述 Python 等效的 Javascript 语句是什么print

采纳答案by Grimtech

You are sending three arguments to console.log

您正在向 console.log 发送三个参数

console.log([1,2],'\n',[3,4])

Because the first argument, [1,2]doesn't contain formatting elements (e.g. %d), each argument is passed through util.inspect(). util.inspect is returning the string representation of '\n'which is not what you want. You want '\n'to be interpreted.

因为第一个参数[1,2]不包含格式化元素(例如%d),所以每个参数都通过util.inspect()传递。util.inspect 返回的字符串表示'\n'不是您想要的。你想'\n'被解释。

One solution is to concatenate all arguments into one string

一种解决方案是将所有参数连接成一个字符串

> console.log([1,2]+'\n'+[3,4]);
1,2
3,4

Another is to use formatting elements as placeholders, which util.format will substitute with two array's converted values.

另一种是使用格式化元素作为占位符,util.format 将替换为两个数组的转换值。

> console.log('%s\n%s', [1,2], [3,4]);
1,2
3,4

I assumed node.js here, but the result in Mozilla is identical.

我在这里假设了 node.js,但在 Mozilla 中的结果是相同的。

回答by zmbq

To be safe, just split it into multiple console.logcalls.

为安全起见,只需将其拆分为多个console.log调用即可。

回答by Roshan Yadav

Use this: print ([1,2], "\'\n'", [3,4]) Here backslash denotes that \n and ' ' shouldn't perform it's operation and print as it is. Use a \ before \n and ' '

使用这个: print ([1,2], "\'\n'", [3,4]) 这里的反斜杠表示 \n 和 ' ' 不应该执行它的操作并按原样打印。在 \n 和 ' ' 之前使用 \