Class 8 String Function
Class 8 String Function
1.strip():-The strip method removes the white spaces from the begining and the end.
ex:-
>>> s = " scodeen "
>>> s.strip()
'scodeen'ex:-
l = [10,56.87,7+9j,True,'string',[1,2,3,4],10]
>>> l
[10, 56.87, (7+9j), True, 'string', [1, 2, 3, 4]]
>>>
Note:- But in python3 we have only input() method and eaw_input() method is not
available.
python 3 input() function behaviour exactly same as raw_input() method of py2 that
is every input value is treated as str type only.
'SCODEEN'
ex:-
>>> s = 'jello'
>>> s.replace('j','h')
'hello'
>>> s = 'jello'
>>> s.replace('jello','hello')
'hello'
>>>
5.split():- The split() method splits the string into substring if it finds
instances of the separator.
>>> s = 'one,two,three,four'
>>> s.split(',')
['one', 'two', 'three', 'four']
ex:-
>>> s = 'scodeen'
>>> len(s)
7
>>> s = 'I love apple,apple is the best'
>>> s.count('apple')
2
>>> s.count('p')
4
>>> s.count('I')
1
ex:-
>>> s = 'I love apple,apple is the best.'
>>> s.endswith('.')
True
>>> s.endswith(',')
False
>>>
ex:-
>>> s = 'I love apple,apple is the best.'
>>> s.startswith('I')
True
>>> s.startswith('J')
False
>>>
9.find/index():- searchs the string for a specified values and returns the position
of where it was found.
11.format():- The format() method takes the passed arrguments,formet then and
places them in the string where are placeholder {} are.
ex:-
>>> s = 'I want {} pieces of the item {} for {} dollar'
>>> s.format('3','567','94.34')
'I want 3 pieces of the item 567 for 94.34 dollar'
>>> l= ['sa','re','ga','ma','pa','dha','nisa']
>>> k = '-'.join(l)
>>> k
'sa-re-ga-ma-pa-dha-nisa'