Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

string function:-

'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', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill'

Important function of string:-

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.

Note:- raw_input function of python 2 is renamed as input() function in python3.

2.lower():- The lower method returns the string in lower case.


ex:-
>> s = 'ScoDeeN'
>>> s.lower()
'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]]
>>>

3.upper():-The upper methods returns the string in upper case.


ex:-
>>> s = 'ScoDeeN'
>>> s.upper()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]]
>>>

'SCODEEN'

4.replace():-The replace method replace a string with another string.

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

6.count():-Returns the number of times a specified values accures in a string.

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

7.endswith():-Retuns the true if string ends with the specified values.

ex:-
>>> s = 'I love apple,apple is the best.'
>>> s.endswith('.')
True
>>> s.endswith(',')
False
>>>

8.startswith():-Retuns the true if string strats with the specified values.

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.

>>> s = 'yes it was'


>>> s.find('i')
4
>>> s.index('i')
4
>>>

10 swapcase():-Swapcases lowwer case become upper and vis-versa.


ex:-
>>> s = 'I Love apple, It"s very testy'
>>> s.swapcase()
'i lOVE APPLE, iT"S VERY TESTY'

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'

12.join:- Join the two substring and convert a string.

>>> l= ['sa','re','ga','ma','pa','dha','nisa']
>>> k = '-'.join(l)
>>> k
'sa-re-ga-ma-pa-dha-nisa'

You might also like