In [3]:
s = 'Hello World!'
In [4]:
str(s)
Out[4]:
In [5]:
repr(s)
Out[5]:
In [6]:
str(1/7)
Out[6]:
In [10]:
repr(1/7)
Out[10]:
In [9]:
x = 10 * 3.25
y = 200 * 200
s = 'the value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
print(s)
In [12]:
hello = 'Hello, World\n'
hellos = repr(hello)
print(hellos)
In [13]:
repr((x,y,('spam','eggs')))
Out[13]:
In [17]:
for x in range(1, 11):
print(repr(x).rjust(2), repr(x*x).rjust(3), end='')
print(repr(x*x*x).rjust(4))
In [2]:
for x in range(1,11):
print('{0:2d} {1:3d} {2:4d}'.format(x,x*x,x*x*x))
In [3]:
'12'.zfill(5)
Out[3]:
In [4]:
'-3.14'.zfill(7)
Out[4]:
In [5]:
'3.14159280098'.zfill(5)
Out[5]:
In [6]:
print('we are the {} who say"{}!"'.format('knight','ni'))
In [7]:
print('{0} and {1}'.format('spam','eggs'))
In [8]:
print('{1} and {0}'.format('spam','eggs'))
In [9]:
print('this {food} is {adj}'.format(food = 'spam', adj = 'delicious'))
In [10]:
print('the story of {0},{1}, and {others}.'.format('bill','tom', others = 'gates'))
In [13]:
import math
print('the value of PI is {}'.format(math.pi))
In [12]:
print('the value of PI is {!r}'.format(math.pi))
In [14]:
print('the value of PI is {0:.3f}'.format(math.pi))
In [27]:
table = {'Mike': 4113, 'Tom': 9918, 'Jack': 5098}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
In [28]:
table = {'Mike': 4413, 'Tom': 9918, 'Jack': 5098}
print('Mike:{0[Mike]:d}; Tom:{0[Tom]:d}; Jack:{0[Jack]:d}'.format(table))
In [30]:
table = {'Mike': 4413, 'Tom': 9918, 'Jack': 5098}
print('Mike:{Mike:d}; Tom:{Tom:d}; Jack:{Jack:d}'.format(**table))
In [41]:
f = open('f.py','r')
In [42]:
f.read()
Out[42]:
In [43]:
f.readline()
Out[43]:
In [51]:
f = open('f.py','r')
In [52]:
f.readlines()
Out[52]:
In [50]:
list(f)
Out[50]:
In [47]:
for line in f:
print(line, end='')
In [54]:
f.write('#this is a test')
In [55]:
f = open('workfile', 'rb+')
f.write(b'0123456789abcdef')
Out[55]:
In [56]:
f.seek(7)
Out[56]:
In [57]:
f.read(1)
Out[57]:
In [61]:
f.seek(13)
Out[61]:
In [62]:
with open('workfile', 'r') as f:
read_data = f.read()
f.closed
Out[62]: