python - python打印语句中逗号和加号有什么区别?

标签 python python-3.x

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

2年前关闭。




Improve this question




我想了解 print((x,y)*5) 的输出.我想知道幕后到底发生了什么。 (对我来说,它似乎变成了一个列表。我说得对吗?)

x = 'Thank'
y = 'You'

print(x+y)
print(x,y)

print((x+y)*5) 
print((x,y)*5) #This one..

我是 Python 的初学者,这是我第一次提出问题。如果这个问题看起来很幼稚,请原谅我。我试过谷歌搜索,但没有帮助。

最佳答案

x = 'Thank'
y = 'You'


# concatenation of string x and y which is treated as a single element and then 
# printed (Here, only a single argument is passed to the print function).
print(x+y)  
# Output: ThankYou  (Simple String concatenation)


# Here, two different arguments are passed to the print function.
print(x,y)  
# Output python 2: ('Thank', 'You')  (x and y are treated as tuple
# Output python 3: Thank You  (x and y are comma seperated and hence, 
# considered two different elements - the default 'sep=" "' is applied.)


# The concatenated result of (x + y) is printed 5 times.
print((x+y)*5) 
# Output: ThankYouThankYouThankYouThankYouThankYou  

# x and y are made elements of a tuple and the tuple is printed 5 times.
print((x,y)*5) 
# Output: ('Thank', 'You', 'Thank', 'You', 'Thank', 'You', 'Thank', 'You', 'Thank', 'You')  

关于python - python打印语句中逗号和加号有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663742/

相关文章:

python - 当我将模型数据映射到一起时,为什么我看不到此 Pyside 表单中的模型数据?

python - 部分正则表达式匹配

python - 在 vim 全方位完成弹出窗口中向下滚动

python - 根据实例化的是异步实例还是同步实例返回包装器

带有 Windows 共享文件夹的 Python3

python - 安装的 Python 脚本无法导入包模块

python - 通过 pyusb 从 USB 鼠标(单芯片,ADNS-2700)获取图像

python - 在 Python 中将非英语单词转换为 % 分隔的字符串

python-3.x - 使用 pandas dataframe 列值来透视其他列

python - 如何从 Bokeh ColumnDatasource 中提取数据