Strings: Built-In Functions
Strings: Built-In Functions
Strings
Strings are ordered text based data which are represented by enclosing the same in
single/double/triple quotes.
In [4]:
String0 = 'Taj Mahal is beautiful'
is
beautiful'''
In [5]:
print (String0 , type(String0))
Taj Mahal
is
String Indexing and Slicing are similar to Lists which was explained in detail earlier.
In [6]:
print (String0[4])
print (String0[4:])
Mahal is beautiful
Built-in Functions
find( ) function returns the index value of the given data that is to found in the string. If it is not
found it returns -1. Remember to not confuse the returned -1 for reverse indexing value.
In [7]:
String0
In [8]:
print (String0.find('al'))
print (String0.find('sadasam'))
-1
The index value returned is the index of the first element in the input data.
In [9]:
print (String0[7])
One can also input find( ) function between which index values it has to search.
In [10]:
String0
print (String0.find('j',1,3))
In [12]:
String3 = ('observe the first letter in this sentence.')
print (String3.capitalize())
center( ) is used to center align the string by specifying the field width.
In [13]:
String0.center(70)
One can also fill the left out spaces with any other character.
In [14]:
String0.center(70,'-')
In [15]:
String0.zfill(40)
expandtabs( ) allows you to change the spacing of the tab character. '\t' which is by default set
to 8 spaces.
In [16]:
s = 'h\te\tl\tl\to'
print (s)
print (s.expandtabs(1))
print (s.expandtabs())
h e l l o
h e l l o
h e l l o
index( ) works the same way as find( ) function the only difference is find returns '-1' when the
input element is not found in the string but index( ) function throws a ValueError
In [17]:
print (String0.index('Taj'))
print (String0.index('Mahal',0))
endswith( ) function is used to check if the given string ends with the particular char which is
given as input.
In [18]:
print (String0.endswith('y'))
False
In [19]:
print (String0.endswith('l',0))
print (String0.endswith('M',0,5))
True
True
count( ) function counts the number of char in the given string. The start and the stop index can
also be specified or left blank. (These are Implicit arguments which will be dealt in functions)
In [20]:
print (String0.count('a',0))
print (String0.count('a',5,10))
join( ) function is used add a char in between the elements of the input string.
In [21]:
'a'.join('*_-')
Out[21]: '*a_a-'
'*_-' is the input string and char 'a' is added in between each element
In [25]:
a = list(String0)
print (a)
b = ''.join(a)
print (b)
['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a',
'u', 't', 'i', 'f', 'u', 'l']
Before converting it into a string join( ) function can be used to insert any char in between the
list elements.
In [26]:
c = '/'.join(a)[18:]
print (c)
/i/s/ /b/e/a/u/t/i/f/u/l
split( ) function is used to convert a string back to a list. Think of it as the opposite of the join()
function.
In [29]:
c = "python\is\powerful\language"
In [37]:
d = c.split(" \ ")
print (d)
In split( ) function one can also specify the number of times you want to split the string or the
number of elements the new returned list should conatin. The number of elements is always one
more than the specified number this is because it is split the number of times specified.
In [38]: e = c.split('/',2)
print (e)
print (len(e))
In [39]:
print (String0)
print (String0.lower())
In [40]:
String0.upper()
In [41]:
String0.replace('Taj Mahal','Bengaluru')
strip( ) function is used to delete elements from the right end and the left end which is not
required.
In [42]:
f = ' hello '
If no char is specified then it will delete all the spaces that is present in the right and left hand
side of the data.
In [43]:
f.strip()
Out[43]: 'hello'
strip( ) function, when a char is specified then it deletes that char if it is present in the two ends
of the specified string.
In [44]:
f = ' ***----hello---******* '
In [45]:
f.strip('*')
The asterisk had to be deleted but is not. This is because there is a space in both the right and
left hand side. So in strip function. The characters need to be inputted in the specific order in
which they are present.
In [46]:
print (f.strip(' *'))
----hello---
hello
lstrip( ) and rstrip( ) function have the same functionality as strip function but the only
difference is lstrip( ) deletes only towards the left side and rstrip( ) towards the right.
In [47]:
print (f.lstrip(' *'))
----hello---*******
***----hello---
Dictionaries
Dictionaries are more used like a database because here you can index a particular sequence
with your user defined string.
In [48]:
d0 = {}
d1 = dict()
Dictionary works somewhat like a list but with an added capability of assigning it's own index
style.
In [49]:
d0['One'] = 1
d0['OneTwo'] = 12
print (d0)
That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'
In [50]:
print (d0['One'])
In [54]:
names = ['One', 'Two', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]
In [55]:
d2 = zip(names,numbers)
print (d2)
The two lists are combined to form a single list and each elements are clubbed with their
respective elements from the other list inside a tuple. Tuples because that is what is assigned
and the value should not change.
In [56]:
localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 5/6
6/10/2021 04
a1 = dict(d2)
print (a1)
Built-in Functions
clear( ) function is used to erase the entire database that was created.
In [57]:
a1.clear()
print (a1)
{}
In [58]:
for i in range(len(names)):
a1[names[i]] = numbers[i]
print (a1)
values( ) function returns a list with all the assigned values in the dictionary.
In [59]:
a1.values()
keys( ) function returns all the index or the keys to which contains the values that it was
assigned to.
In [60]:
a1.keys()
items( ) is returns a list containing both the list but each element in the dictionary is inside a
tuple. This is same as the result that was obtained when zip function was used.
In [61]:
a1.items()
Out[61]: dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)])
pop( ) function is used to get the remove that particular element and this removed element can
be assigned to a new variable. But remember only the value is stored and not the key. Because
the is just a index value.
In [62]:
a2 = a1.pop('Four')
print (a1)
print (a2)
In [ ]: