Python打印与Javascript console.log() [英] Python print vs Javascript console.log()

查看:889
本文介绍了Python打印与Javascript console.log()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中:

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

将打印

[1,2]
[3,4]

在Javascript中:

In Javascript:

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

打印

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

上述Python的等效Javascript语句 print

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

推荐答案

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

You are sending three arguments to console.log

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

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

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.

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

One solution is to concatenate all arguments into one string

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

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

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

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

我假设node.js在这里,但是Mozilla的结果是一样的。

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

这篇关于Python打印与Javascript console.log()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆