Professional Documents
Culture Documents
Strings
Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes.
In [ ]: print(String0); print(type(String0))
print (String1); print(type(String1))
print (String2); print(type(String2))
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.
The index value returned is the index of the first element in the input data.
In [ ]: print (String0[7])
One can also input find( ) function between which index values it has to search.
In [ ]: print (String0.find('j'))
print (String0.find('a',2,6))
center( ) is used to center align the string by specifying the field width.
In [ ]: String0.center(70)
One can also fill the left out spaces with any other character.
In [ ]: String0.center(70,'-')
In [ ]: String0.zfill(26)
expandtabs( ) allows you to change the spacing of the tab character. '\t' which is by default set to 8 spaces.
In [ ]: s = 'h\ne\tl\tl\to'
print (s)
print (s.expandtabs(1))
print (s.expandtabs())
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
endswith( ) function is used to check if the given string ends with the particular char which is given as input.
In [ ]: print (String0.endswith('y'))
In [ ]: print (String0.endswith('l',0))
print (String0.endswith('M',0,5))
In [ ]: s='a'
ord(s)
In [ ]: chr(65)
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 [ ]: print (String0.count('a'))
print (String0.count('a',5,10))
join( ) function is used add a char in between the elements of the input string.
In [ ]: 'a'.join('*_-')
In [ ]:
In [ ]: lst[0]=56
In [ ]: lst
In [ ]: lst.pop()
In [ ]: lst
In [ ]: str='Excellent'
lst=list(str);print(lst);print(type(lst))
In [ ]: print(' '.join(lst))
In [ ]:
'*_-' is the input string and char 'a' is added in between each element
In [ ]: a = list(String0)
print (a)
b = ''.join(a)
print (b)
Before converting it into a string join( ) function can be used to insert any char in between the list elements.
In [ ]: c = '/'.join(a)[18:]
print (c)
split( ) function is used to convert a string back to a list. Think of it as the opposite of the join() function.
In [ ]: 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 [ ]: e = c.split('/',3)
print (e)
print (len(e))
In [ ]: String0.upper()
In [ ]: String0.replace('Taj Mahal','Bengaluru')
In [ ]: print(String0)
strip( ) function is used to delete elements from the right end and the left end which is not required.
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 [ ]: f.strip()
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 [ ]: 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.
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 [ ]: print(f)
print (f.lstrip('* '))
print (f.rstrip(' *'))
In [ ]: str=u"\u0925\u0935"
print(str)
In [ ]: lst=[5, 10]
x=bytes(lst); print(x)