In [3]:
s = 'Hello World!'

In [4]:
str(s)


Out[4]:
'Hello World!'

In [5]:
repr(s)


Out[5]:
"'Hello World!'"

In [6]:
str(1/7)


Out[6]:
'0.14285714285714285'

In [10]:
repr(1/7)


Out[10]:
'0.14285714285714285'

In [9]:
x = 10 * 3.25
y = 200 * 200
s = 'the value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
print(s)


the value of x is 32.5, and y is 40000...

In [12]:
hello = 'Hello, World\n'
hellos = repr(hello)
print(hellos)


'Hello, World\n'

In [13]:
repr((x,y,('spam','eggs')))


Out[13]:
"(32.5, 40000, ('spam', 'eggs'))"

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))


 1   1   1
 2   4   8
 3   9  27
 4  16  64
 5  25 125
 6  36 216
 7  49 343
 8  64 512
 9  81 729
10 1001000

In [2]:
for x in range(1,11):
    print('{0:2d} {1:3d} {2:4d}'.format(x,x*x,x*x*x))


 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

In [3]:
'12'.zfill(5)


Out[3]:
'00012'

In [4]:
'-3.14'.zfill(7)


Out[4]:
'-003.14'

In [5]:
'3.14159280098'.zfill(5)


Out[5]:
'3.14159280098'

In [6]:
print('we are the {} who say"{}!"'.format('knight','ni'))


we are the knight who say"ni!"

In [7]:
print('{0} and {1}'.format('spam','eggs'))


spam and eggs

In [8]:
print('{1} and {0}'.format('spam','eggs'))


eggs and spam

In [9]:
print('this {food} is {adj}'.format(food = 'spam', adj = 'delicious'))


this spam is delicious

In [10]:
print('the story of {0},{1}, and {others}.'.format('bill','tom', others = 'gates'))


the story of bill,tom, and gates.

In [13]:
import math
print('the value of PI is {}'.format(math.pi))


the value of PI is 3.141592653589793

In [12]:
print('the value of PI is {!r}'.format(math.pi))


the value of PI is 3.141592653589793

In [14]:
print('the value of PI is {0:.3f}'.format(math.pi))


the value of PI is 3.142

In [27]:
table = {'Mike': 4113, 'Tom': 9918, 'Jack': 5098}
for name, phone in table.items():
    print('{0:10} ==> {1:10d}'.format(name, phone))


Tom        ==>       9918
Jack       ==>       5098
Mike       ==>       4113

In [28]:
table = {'Mike': 4413, 'Tom': 9918, 'Jack': 5098}
print('Mike:{0[Mike]:d}; Tom:{0[Tom]:d}; Jack:{0[Jack]:d}'.format(table))


Mike:4413; Tom:9918; Jack:5098

In [30]:
table = {'Mike': 4413, 'Tom': 9918, 'Jack': 5098}
print('Mike:{Mike:d}; Tom:{Tom:d}; Jack:{Jack:d}'.format(**table))


Mike:4413; Tom:9918; Jack:5098

In [41]:
f = open('f.py','r')

In [42]:
f.read()


Out[42]:
"def fib(n):    # write Fibonacci series up to n\n    a, b = 0, 1\n    while b < n:\n        print(b, end=' ')\n        a, b = b, a+b\n    print()\n\ndef fib2(n): # return Fibonacci series up to n\n    result = []\n    a, b = 0, 1\n    while b < n:\n        result.append(b)\n        a, b = b, a+b\n    return result\n"

In [43]:
f.readline()


Out[43]:
''

In [51]:
f = open('f.py','r')

In [52]:
f.readlines()


Out[52]:
['def fib(n):    # write Fibonacci series up to n\n',
 '    a, b = 0, 1\n',
 '    while b < n:\n',
 "        print(b, end=' ')\n",
 '        a, b = b, a+b\n',
 '    print()\n',
 '\n',
 'def fib2(n): # return Fibonacci series up to n\n',
 '    result = []\n',
 '    a, b = 0, 1\n',
 '    while b < n:\n',
 '        result.append(b)\n',
 '        a, b = b, a+b\n',
 '    return result\n']

In [50]:
list(f)


Out[50]:
['def fib(n):    # write Fibonacci series up to n\n',
 '    a, b = 0, 1\n',
 '    while b < n:\n',
 "        print(b, end=' ')\n",
 '        a, b = b, a+b\n',
 '    print()\n',
 '\n',
 'def fib2(n): # return Fibonacci series up to n\n',
 '    result = []\n',
 '    a, b = 0, 1\n',
 '    while b < n:\n',
 '        result.append(b)\n',
 '        a, b = b, a+b\n',
 '    return result\n']

In [47]:
for line in f:
    print(line, end='')


def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

In [54]:
f.write('#this is a test')


---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-54-bc0cdacc1b31> in <module>()
----> 1 f.write('#this is a test')

UnsupportedOperation: not writable

In [55]:
f = open('workfile', 'rb+')
f.write(b'0123456789abcdef')


Out[55]:
16

In [56]:
f.seek(7)


Out[56]:
7

In [57]:
f.read(1)


Out[57]:
b'7'

In [61]:
f.seek(13)


Out[61]:
13

In [62]:
with open('workfile', 'r') as f:
    read_data = f.read()
f.closed


Out[62]:
True