如何在没有方括号的同一行上打印字符串和列表

时间:2016-06-12 10:21:13

标签: python

l = [1, 2]
print "My numbers are:", l

这给了我

My numbers are: [1, 2]

但我想要

My numbers are: 1, 2

我见过','.join(l),但我不知道如何在同一行打印出所有内容。

3 个答案:

答案 0 :(得分:2)

要使.join工作,您必须将每个元素都更改为str

print ', '.join(map(str, l))

答案 1 :(得分:0)

在这里,我使用生成器生成字符串值的列表,然后我将它们连接在一起!我希望这有帮助!

l = [1, 2]
print 'My numbers are: {}'.format(' and '.join(str(x) for x in l))

答案 2 :(得分:0)

@charset"utf-8";

/* CSS Document */

body {
  margin: 30px;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.8em;
  color: #333;
}
#content {
  margin: 80px 70px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}
/* Header/footer boxes */

.wideBox {
  clear: both;
  text-align: center;
  margin: 70px;
  padding: 10px;
  background: #ebedf2;
  border: 1px solid #333;
}
.wideBox h1 {
  font-weight: bold;
  margin: 20px;
  color: #666;
  font-size: 1.5em;
}
#cardSlots {
  margin: 50px auto 0 auto;
}
#cardPile {
  margin: 0 auto;
}
#cardPile {
  width: 1200px;
  height: 120px;
  padding: 20px;
}
#cardSlots {
  width: 1200px;
  height: 1000px;
  padding: 20px;
  font-family: Arial, Helvetica, sans-serif;
  line-height: normal;
}
#cardSlots div,
#cardPile div {
  float: left;
  width: 80px;
  height: 50px;
  padding: 10px;
  padding-bottom: 0;
  border: 1px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  margin: 0 0 0 10px;
  background: #fff;
}
#cardSlots div:first-child,
#cardPile div:first-child {
  margin-left: 0;
}
#cardSlots div.hovered {
  background: #aaa;
}
#cardSlots div {
  border: 1px dashed #333;
}
#cardPile div {
  background: #fff;
  border: 1px solid #005575;
  color: #000;
  font-size: 11px;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.3;
}
#cardPile div.ui-draggable-dragging {
  -moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
}
#card1.correct {
  background: #5fcf80;
  color: #fff;
}

使用条带。转换字符串中的列表,然后删除要在strip函数中指定它们的char。