In [1]:
message = str('Hello Python')

In [2]:
message


Out[2]:
'Hello Python'

In [3]:
type(message)


Out[3]:
str

In [4]:
type(str)


Out[4]:
type

In [5]:
isinstance(str, type)


Out[5]:
True

In [6]:
isinstance(message, str)


Out[6]:
True

In [7]:
message.upper()


Out[7]:
'HELLO PYTHON'

In [9]:
message.title()


Out[9]:
'Hello Python'

In [10]:
message.__len__()


Out[10]:
12

In [11]:
len(message)


Out[11]:
12

In [12]:
id(message)


Out[12]:
87164032L

In [19]:
hex(id(message))


Out[19]:
'0x5320480L'

In [20]:
message.__getitem__(0)


Out[20]:
'H'

In [21]:
message[0]


Out[21]:
'H'

In [22]:
dir(str)


Out[22]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_formatter_field_name_split',
 '_formatter_parser',
 'capitalize',
 'center',
 'count',
 'decode',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'index',
 'isalnum',
 'isalpha',
 'isdigit',
 'islower',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

Other useful and interesting builtins in Python


In [23]:
chr(65)


Out[23]:
'A'

In [24]:
ord('a')


Out[24]:
97

Let us combine two or more builtins to perform some simple magic.

  • map applies the function passed as the first argument for each of the elements in the second argument and returns a list object containing the results of the function application.
  • range is used to generate a sequence of integers over the interval [start, end)

In [25]:
map(ord, 'abcde')


Out[25]:
[97, 98, 99, 100, 101]

In [26]:
map(chr, range(65, 65+26))


Out[26]:
['A',
 'B',
 'C',
 'D',
 'E',
 'F',
 'G',
 'H',
 'I',
 'J',
 'K',
 'L',
 'M',
 'N',
 'O',
 'P',
 'Q',
 'R',
 'S',
 'T',
 'U',
 'V',
 'W',
 'X',
 'Y',
 'Z']

Let us look at some integer magic using various bases (decimal, binary, octal, hexadecimal)


In [27]:
bin(255)


Out[27]:
'0b11111111'

In [28]:
hex(255)


Out[28]:
'0xff'

In [29]:
int('0b00111111', base=2)


Out[29]:
63

In [ ]: