如何转换字符串 [英] how to convert string
问题描述
我想在这样的一行打印0到9号
0 1 2 3 4 5 6 7 8 9
如果我愿意的话这个,它在不同的行中打印
for x in xrange(10):
打印我
所以我试过这样的事情
str =""
$ x $ b for x in xrange(10):
str = i + "
print str
但我想知道如何将int转换为字符串。
每个帮助非常感谢。
I want to print number 0 to 9 in one line like this
0 1 2 3 4 5 6 7 8 9
if I do like this, it prints in different lines
for i in xrange(10):
print i
so i tried like this
str = ""
for i in xrange(10):
str = i + " "
print str
but i want to know how convert int i to string.
Every help is appreciate.
推荐答案
di********@gmail.com 写道:
我想在这样的一行中打印0到9号
0 1 2 3 4 5 6 7 8 9
如果我喜欢这样,它会以不同的线条打印
for x in xrange(10):
print i
for x in xrange(10):
打印i,
所以我试过这样的
str =" ;对于我在xrange(10)中的
:
str = i +
print str
但我想知道如何将int i转换为字符串。
I want to print number 0 to 9 in one line like this
0 1 2 3 4 5 6 7 8 9
if I do like this, it prints in different lines
for i in xrange(10):
print i
for i in xrange(10):
print i,
so i tried like this
str = ""
for i in xrange(10):
str = i + " "
print str
but i want to know how convert int i to string.
s ="" #不要遮挡内置的str。
$ x $ b for x in xrange(10):
s + = str(i)+"
print s
s = "" # Don''t shadow the str builtin.
for i in xrange(10):
s += str(i) + " "
print s
di ********@gmail.com 写道:
di********@gmail.com wrote:
我想在这样的一行中打印0到9号
0 1 2 3 4 5 6 7 8 9
如果我喜欢这样,它会以不同的线条打印
我在xrange(10):
print i
所以我尝试了这样的
str =""
for x in xrange(10):
str = i +"
print str
但我想知道如何将int转换为字符串。
I want to print number 0 to 9 in one line like this
0 1 2 3 4 5 6 7 8 9
if I do like this, it prints in different lines
for i in xrange(10):
print i
so i tried like this
str = ""
for i in xrange(10):
str = i + " "
print str
but i want to know how convert int i to string.
转换函数名为str (),所以你可能
想要重命名那个变量...
out =""
for i在xrange(10):
out = out + str(i)+"
打印出来
另一方面,因为打印在同一行打印的项目之间增加了空格
,你可以简单地为我在范围内(10)做
:
打印我,
打印
>
或者你可以使用加入,比如
print" " .join(str(i)for i in range(10))
或
print" " .join(map(str,range(10)))
< / F>
the conversion function is called str(), so you might
want to rename that variable...
out = ""
for i in xrange(10):
out = out + str(i) + " "
print out
on the other hand, since print adds spaces between items printed
on the same line, you can simply do
for i in range(10):
print i,
print
or you could use join, like
print " ".join(str(i) for i in range(10))
or
print " ".join(map(str, range(10)))
</F>
di********@gmail.com 写道:
di********@gmail.com wrote:
我想在这样的一行中打印0到9号
0 1 2 3 4 5 6 7 8 9
如果我喜欢这样,它会以不同的行打印
我在xrange(10):
打印我
打印结束时的逗号会做你想要的:
for x in xrange(10):
打印i,
所以我试过这样的
str =""
for x in xrange(10):
str = i +"
print str
但我想知道如何将int转换为字符串。
str(i)将做你想要的和那么%也是如此运营商。但是,你的
示例代码掩盖了内置的str。函数通过创建一个变量与
相同的名称(加上你在该行中有另一个错误),所以试试这个:
s =""
for x in xrange(10):
s = s + str(i)+"
print s
循环中行的快捷方式是
s + = str(i)+"
另请注意,附加到列表时附加到字符串的速度很慢。因此,尝试构建一个列表并将其转换为字符串join的字符串。像这样的方法
l = []
$ x $ b for x in xrange(10):
l.append(str(i)
print"" .join(l)
最后,您可以使用列表推导构造构建列表
l = [str( i)for x in xrange(10)]
print" .join(l)
您还可以将上述两行合并为一行 - 那会更短,但可能不会更清楚。
欢呼,
加里赫伦
每一个帮助都是值得欣赏的。
I want to print number 0 to 9 in one line like this
0 1 2 3 4 5 6 7 8 9
if I do like this, it prints in different lines
for i in xrange(10):
print i
A comma at the end of the print will do what you want:
for i in xrange(10):
print i,
so i tried like this
str = ""
for i in xrange(10):
str = i + " "
print str
but i want to know how convert int i to string.
str(i) will do what you want and so will the "%" operator. However, you
sample code masks the builtin "str" function by creating a variable with
the same name (plus you had another error in that line), so try this:
s = ""
for i in xrange(10):
s = s + str(i) + " "
print s
A shortcut for the line in the loop is
s += str(i) + " "
Also note that appending to string is slow while appending to lists is not. So try build a list and turning it into a string with the string "join" method like this
l = []
for i in xrange(10):
l.append(str(i)
print " ".join(l)
Finally, you can build the list with a list comprehension construct
l = [str(i) for i in xrange(10)]
print " ".join(l)
You could also combine the above two lines into one -- that would be shorter, but probably not clearer.
Cheers,
Gary Herron
Every help is appreciate.
这篇关于如何转换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!