また、dir() を使うとオブジェクトが持っている属性(内部データ、メソッド関数)を 調べることができ、type() を使うと「型」を調べることができる。
>>> code = "x = 'Something'"
>>> x = "Nothing"
>>> exec code
>>> x
'Something'
>>> dict = {'x': 'Anything'}
>>> code = "print x"
>>> exec code in dict
Anything
>>> z = eval("'xo'*10")
>>> z
'xoxoxoxoxoxoxoxoxoxo'
>>> a = 'tomato'
>>> a.capitalize()
'Tomato'
>>> a = 'tomato\tpotato'
>>> a
'tomato\tpotato'
>>> print a
tomato potato
>>> a.expandtabs()
'tomato potato'
>>> a = 'now is the time'
>>> a.find('is')
4
>>> a.count('i')
2
>>> a.replace(' ', '_')
'now_is_the_time'
>>> a.split(' ')
['now', 'is', 'the', 'time']
>>> x = '*'
>>> x.join(a.split(' '))
'now*is*the*time'
>>> " before and after ".strip()
'before and after'
>>> import re
>>> s = 'abc def 012 xyz'
>>> re.match('a.*z',s)
<_sre.SRE_Match object at 0x00A98020>
>>> re.match('abc',s)
<_sre.SRE_Match object at 0x00AA0020>
>>> re.match('def',s)
>>> re.search('def',s)
<_sre.SRE_Match object at 0x00AA6140>
>>> x = re.search('def',s)
>>> x.start(), x.end()
(4, 7)
>>> s[x.start():x.end()]
'def'
>>> re.sub('def','rst',s)
'abc rst 012 xyz'
>>> re.sub('\d+','',s)
'abc def xyz'
>>> y = re.match('.* (\d+) xyz',s)
>>> y.group(1)
'012'
>>> re.sub('^.* (\d+) xyz','\g<1> yen',s)
'012 yen'
>>> import string
>>> string.atoi(y.group(1))
12
主な Special Characters (特殊文字)
>>> s
'abc def 012 xyz'
>>> r = re.compile('^.* (\d+) xyz$')
>>> r.sub('\g<1> yen',s)
'012 yen'
>>> t = 'qwe asd ert 345 xyz'
>>> r.sub('\g<1> yen', t)
'345 yen'
>>> import time
>>> time.asctime(time.localtime())
'Sun Jan 13 15:42:26 2002'
>>> time.time()
1010904182.54
>>> t = time.time()
>>> u = time.localtime(t)
>>> u
(2002, 1, 13, 15, 43, 40, 6, 13, 0)
>>> g = time.gmtime(t)
>>> g
(2002, 1, 13, 6, 43, 40, 6, 13, 0)
>>> time.asctime(g)
'Sun Jan 13 06:43:40 2002'
>>> time.strftime('%Y/%m/%d %H:%M:%S',u)
'2002/01/13 15:43:40'
>>> def test():
a = time.time()
time.sleep(20)
b = time.time()
print b - a
>>> test()
19.9900000095