Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
175 views

Class 8 String Function

The document lists and describes 12 important string functions in Python: strip(), lower(), upper(), replace(), split(), count(), endswith(), startswith(), find/index(), swapcase(), format(), and join. These functions allow manipulating strings by removing/adding whitespace, changing case, finding/replacing substrings, counting occurrences, and joining substrings into a single string.

Uploaded by

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

Class 8 String Function

The document lists and describes 12 important string functions in Python: strip(), lower(), upper(), replace(), split(), count(), endswith(), startswith(), find/index(), swapcase(), format(), and join. These functions allow manipulating strings by removing/adding whitespace, changing case, finding/replacing substrings, counting occurrences, and joining substrings into a single string.

Uploaded by

kunala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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