0% found this document useful (0 votes)
59 views6 pages

Strings: Built-In Functions

Strings are ordered text data that can be represented using single, double, or triple quotes. Strings have many built-in functions like find(), capitalize(), center(), zfill(), expandtabs(), index(), endswith(), count(), join(), split(), lower(), upper(), replace(), strip(), lstrip(), and rstrip() that allow manipulating and processing string data in various ways. Dictionaries are like lists but store data as key-value pairs instead of using indexes.

Uploaded by

Ravi goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views6 pages

Strings: Built-In Functions

Strings are ordered text data that can be represented using single, double, or triple quotes. Strings have many built-in functions like find(), capitalize(), center(), zfill(), expandtabs(), index(), endswith(), count(), join(), split(), lower(), upper(), replace(), strip(), lstrip(), and rstrip() that allow manipulating and processing string data in various ways. Dictionaries are like lists but store data as key-value pairs instead of using indexes.

Uploaded by

Ravi goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/10/2021 04

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'

String1 = "Taj Mahal is beautiful"

String2 = '''Taj Mahal

is

beautiful'''

In [5]:
print (String0 , type(String0))

print (String1, type(String1))

print (String2, type(String2))

Taj Mahal is beautiful <class 'str'>

Taj Mahal is beautiful <class 'str'>

Taj Mahal

is

beautiful <class 'str'>

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

Out[7]: 'Taj Mahal is beautiful'

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

Out[10]: 'Taj Mahal is beautiful'

localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 1/6


6/10/2021 04

In [11]: print (String0.find('j',1))

print (String0.find('j',1,3))

capitalize( ) is used to capitalize the first element in the string.

In [12]:
String3 = ('observe the first letter in this sentence.')

print (String3.capitalize())

Observe the first letter in this sentence.

center( ) is used to center align the string by specifying the field width.

In [13]:
String0.center(70)

Out[13]: ' Taj Mahal is beautiful '

One can also fill the left out spaces with any other character.

In [14]:
String0.center(70,'-')

Out[14]: '------------------------Taj Mahal is beautiful------------------------'

zfill( ) is used for zero padding by specifying the field width.

In [15]:
String0.zfill(40)

Out[15]: '000000000000000000Taj Mahal is beautiful'

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

localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 2/6


6/10/2021 04

The start and stop index values can also be specified.

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

join( ) function can also be used to convert a list into a string.

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']

Taj Mahal is beautiful

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)

['python is powerful language']

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.

localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 3/6


6/10/2021 04

In [38]: e = c.split('/',2)

print (e)

print (len(e))

['python is powerful language']

lower( ) converts any capital letter to small letter.

In [39]:
print (String0)

print (String0.lower())

Taj Mahal is beautiful

taj mahal is beautiful

upper( ) converts any small letter to capital letter.

In [40]:
String0.upper()

Out[40]: 'TAJ MAHAL IS BEAUTIFUL'

replace( ) function replaces the element with another element.

In [41]:
String0.replace('Taj Mahal','Bengaluru')

Out[41]: 'Bengaluru is beautiful'

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('*')

Out[45]: ' ***----hello---******* '

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(' *'))

print (f.strip(' *-'))

localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 4/6


6/10/2021 04

----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(' *'))

print (f.rstrip(' *'))

----hello---*******

***----hello---

Dictionaries
Dictionaries are more used like a database because here you can index a particular sequence
with your user defined string.

To define a dictionary, equate a variable to { } or dict()

In [48]:
d0 = {}

d1 = dict()

print (type(d0), type(d1))

<class 'dict'> <class '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)

{'One': 1, 'OneTwo': 12}

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

Two lists which are related can be merged to form a dictionary.

In [54]:
names = ['One', 'Two', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]

zip( ) function is used to combine two lists

In [55]:
d2 = zip(names,numbers)

print (d2)

<zip object at 0x7f81a906a648>

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.

Further, To convert the above into a dictionary. dict( ) function is used.

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)

{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}

Built-in Functions
clear( ) function is used to erase the entire database that was created.

In [57]:
a1.clear()

print (a1)

{}

Dictionary can also be built using loops.

In [58]:
for i in range(len(names)):

a1[names[i]] = numbers[i]

print (a1)

{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}

values( ) function returns a list with all the assigned values in the dictionary.

In [59]:
a1.values()

Out[59]: dict_values([1, 2, 3, 4, 5])

keys( ) function returns all the index or the keys to which contains the values that it was
assigned to.

In [60]:
a1.keys()

Out[60]: dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])

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)

{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}

In [ ]:

localhost:8888/nbconvert/html/Downloads/Python pavan sir/04.ipynb?download=false 6/6

You might also like