python - 避免数组迭代中的跳行

标签 python python-3.x

在这段小代码中:

print('Your cards are:')
for i in playerCards:
    print(i)

产生我想要的输出:

Your cards are:
TD
6D

但是,我想避免数组上每次迭代之间的行跳转。我怎样才能生产这个呢?

Your cards are: TD 6D

最佳答案

您可以指定 end 关键字参数来打印空格而不是换行符:

print('Your cards are:', end=' ')
for i in playerCards:
    print(i, end=' ')

或者,仅使用一个带有参数解包的 print 函数调用:

print('Your cards are:', *playerCards)

关于python - 避免数组迭代中的跳行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652229/

相关文章:

python-3.x - flask 记录重复输出

python - 为什么这个变量被认为是本地的?

python - 有没有办法使用 python formencode 验证文件大小?

python 3 : reading bytes from stdin pipe with readahead

python - 从命令行运行 matlab 脚本后如何退出?

python - 尝试设置 pandas 列名称时出现 "Bus error: 10"

python - 转换字典列表,根据键组合列表项

python - 属性错误 : module 'regex' has no attribute 'Pattern'

python - 检索 Panda Dataframe 列中列表的最后一个元素

python - __iter__ 是如何工作的?