假设我在/home/ashraful/test.txt
上有一个文件。只是我想打开文件。 现在我的问题是:哪一个是关于python格式化字符串的好习惯?
哪一个是好的做法?
解决方案1:
dir = "/home/ashraful/"
fp = open("{0}{1}".format(dir, 'test.txt'), 'r')
解决方案2:
dir = "/home/ashraful/"
fp = open(dir + 'test.txt', 'r')
两个办法,我可以打开文件。
谢谢:)
当谈到在Python中整合字符串时,我想说的是更具可读性,这是Python的一个原则。 http://docs.python-guide.org/en/latest/writing/style/在这种情况下,如果没有这些更好的选择,它将是第二个。 –