如何在Python中将整数迭代附加到字符串中? [英] How to append a integer iteration into a string in python?
本文介绍了如何在Python中将整数迭代附加到字符串中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的问题:
首先我迭代
for i in range(1,21):
print (i)
然后我有一个变量basename ="Station"
then I have a variable basename="Station"
我想要一个输出为Station_1.txt,Station_2.txt等的结果.
I want a result where the output is Station_1.txt, Station_2.txt etc.
我设法将"_"添加到变量基名中,例如
I managed to add the "_" to the variable basename like
mylist1 = ("_")
s = (basename)
for item in mylist1:
s += item
但是现在我有一个关于如何在变量basename上获取迭代的问题,我可以像"Station_,1,Station_,2"一样获得它的所有权,但不能在字符串本身内部获取.
but now I have the problem on how to get the iteration on top of the variable basename, I could get is as it own like "Station_, 1, Station_, 2" but not inside the string itself.
对不起,这里是个初学者:)
Sorry, a total beginner here :)
推荐答案
您想要这样的东西吗?
basename = "Station"
for i in range(1, 21):
value = basename + "_" + str(i) + ".txt"
print(value)
输出:
Station_1.txt
Station_2.txt
Station_3.txt
Station_4.txt
Station_5.txt
Station_6.txt
Station_7.txt
Station_8.txt
Station_9.txt
Station_10.txt
Station_11.txt
Station_12.txt
Station_13.txt
Station_14.txt
Station_15.txt
Station_16.txt
Station_17.txt
Station_18.txt
Station_19.txt
Station_20.txt
这篇关于如何在Python中将整数迭代附加到字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文