在Python中将int转换为字符串 [英] Casting an int to a string in Python

查看:978
本文介绍了在Python中将int转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够生成一些名称为fileX.txt的文本文件,其中X是一个整数:

I want to be able to generate a number of text files with the names fileX.txt where X is some integer:

for i in range(key):
    filename = "ME" + i + ".txt" //Error here! Can't concat a string and int
    filenum = filename
    filenum = open(filename , 'w')  

有没有其他人知道怎么做filename =ME+我的部分,所以我得到一个名称为ME0.txt,ME1.txt,ME2的文件列表。 txt,等等

Does anyone else know how to do the filename = "ME" + i part so I get a list of files with the names: "ME0.txt" , "ME1.txt" , "ME2.txt" , and etc

推荐答案

x = 1
y = "foo" + str(x)

请参阅Python文档: https://docs.python.org/2/library/functions.html#str

Please see the Python documentation: https://docs.python.org/2/library/functions.html#str

这篇关于在Python中将int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆