List string functions

In [18]:
s = 'abc'
print(dir(s))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Capitilize the first letter

In [2]:
'python is cool'.capitalize()
Out[2]:
'Python is cool'

Center a string in a window of characters

In [3]:
print('Python is cool'.center(50))
                  Python is cool                  

Count a substring in a string

In [4]:
'Python is cool'.count('o')
Out[4]:
3
In [5]:
'Python is cool'.count('oo')
Out[5]:
1

Find the index of a charachter or a substring

In [6]:
'Python is cool'.find('th')
Out[6]:
2
In [7]:
'Python is cool'.find('m')
Out[7]:
-1
In [8]:
'Welcome {} to Python {}'.format('Ahmed', 'II')
Out[8]:
'Welcome Ahmed to Python II'

is title? return true if the 1st letter of every word is capital

In [9]:
'Python Programming'.istitle()
Out[9]:
True
In [10]:
'Python programming'.istitle()
Out[10]:
False

title

In [11]:
'hello world, welcome to python 2'.title()
Out[11]:
'Hello World, Welcome To Python 2'

Partition: works like split but returns a tuple.

In [12]:
'Hello World, welcome to python 2'.partition(' ')
Out[12]:
('Hello', ' ', 'World, welcome to python 2')
In [13]:
x = 3 
y = 4 
point = (x, y) # tuple
print(point)
(3, 4)

Split: returns a list of words

In [14]:
'Hello World, welcome to python 2'.split() # by default split using space 
Out[14]:
['Hello', 'World,', 'welcome', 'to', 'python', '2']
In [15]:
'Hello World, welcome to python 2'.split(',')
Out[15]:
['Hello World', ' welcome to python 2']

Zero fill

In [16]:
'42'.zfill(5)
Out[16]:
'00042'
In [17]:
for i in range(15):
    print(str(i).zfill(6))
000000
000001
000002
000003
000004
000005
000006
000007
000008
000009
000010
000011
000012
000013
000014