>>> 'this is a {} called {}'.format('language','python')
'this is a language called python'
{}内的内容决定了这个位置是什么
>>> 'this is a {} called {}'.format('language','python')
'this is a language called python'
>>> 'this is a {0} called {1}'.format('language','python')
'this is a language called python'
>>> 'this is a {1} called {0}'.format('language','python')
'this is a python called language'
>>> 'this is a {1} called {1}'.format('language','python')
'this is a python called python'
如果{}内出现了关键字,其内容由format后的定义决定
>>> 'my favourite language is {lan}'.format(lan = 'python')
'my favourite language is python'
>>> fileobj = open('fileToTest.hi', 'r')
>>> fileobj.read()
'this is a file to be test by python\nthis is second line\nthis is end line'
>>> fileobj.read()
''
把文件内容读完后,再去读就会返回空字符串
还可以一行一行读
>>> fileobj = open('fileToTest.hi', 'r')
>>> fileobj.readline()
'this is a file to be test by python\n'
>>> fileobj.readline()
'this is second line\n'
>>> fileobj.readline()
'this is end line'
或者返回多行构成的list
>>> fileobj = open('fileToTest.hi', 'r')
>>> fileobj.readlines()
['this is a file to be test by python\n', 'this is second line\n', 'this is end line']
另一种逐行读取的方式是:
>>> fileobj = open('fileToTest.hi', 'r')
>>> for line in fileobj:
... print line
...
this is a file to be test by python
this is second line
this is end line