use*_*525 2 python string python-2.7
我有我的代码:
for i in range(0,5):
print "result is:"+str(i),
Run Code Online (Sandbox Code Playgroud)
这打印:
result is:0 result is:1 result is:2 result is:3 result is:4
Run Code Online (Sandbox Code Playgroud)
我想像这样打印:
result is:0,1,2,3,4
Run Code Online (Sandbox Code Playgroud)
如何在python中做到这一点?
print('Result is: {}'.format(', '.join(map(str, range(5)))))
Run Code Online (Sandbox Code Playgroud)
版画
Result is: 0, 1, 2, 3, 4
Run Code Online (Sandbox Code Playgroud)
适用于Python 2和Python 3.
说明:
range(5)是一样的range(0, 5).map(str, x),给定一个可迭代的x,返回一个新的iterable,其中包含x stringified(e- > str(e))中的所有项.', '.join(iterable)使用给定的分隔符将给定的元素为字符串的iterable(列表,生成器等)连接到字符串1字符串中.最后按顺序用其参数str.format()替换{}占位符.
| 归档时间: |
|
| 查看次数: |
540 次 |
| 最近记录: |