Python STRING Methods & Functions
Python STRING Methods & Functions
Data Type Creation of the data Accessing chr of str Mathematical Operator for str Membership Operator for str
1) s = “ “ # Empty str 1) Indexing 2) Slicing 1) Str concatenation(+) 1) in
2) s = input(‘enter str:’) # Dynamic input - Both +ve & -ve indexing - Accessing a substr - Both should be str type 2) not in
3) s = ‘sri’ # str with known data - s[index no.] # returns the element - s[begin:end:step] # returns substr 2) Str repetation(*) - Return type is Boolean (True/False)
4) s = str( ) # using str function - Index Error if index out of range - Slice operator never gives index error. - Atleat one should be str and other int,
order is not imp
Cheaking starting and ending part
Comparision Operator Finding substrings Spliting of string of str
1) <, <=, >, >= 1) find( ) 1) split( ) 1) s.startswith(‘substr’)
- Compares based on Unicode values - s.find(‘substr’) - s.split( ) 2) s.endswith(‘substr’)
- Compares first different chr - s.find(‘substr’, begin, end) # assigning boundary - Splits based on the argument provided. - Return type is Boolean
2) ==, != - It returns the index of 1st occurrence of the provided substring. - Return tupe of split( ) is List
- Content comparision - Search is from L to R - The default seperator for split( ) is space
- Return type is Boolean - Returns -1 if the specified substr not found
2) rfind( )
Removing spaces from str - s.rfind(‘substr”) Joining the string Cheaking type of chr in a str
1)lstrip( ) # removes left side spaces - s.find(‘substr’, begin, end) # assigning boundary 1) join( ) 1) s.isalnum( ) # (A-Z, a-z, 0-9)
2)rstrip( ) # removes right side spaces - It returns the +ve index of 1st occurrence of the provided substring in reverse - ‘seperator’.join(l) - To check it is Alphanumeric
"STRING" 3)strip( ) # removes spaces of both sides order. - Joins based on the seperator provided 2) s.isalpha( )
A sequence of strip( ) method cannot remove spaces b/w - Search is from R to L within quotation and returns as a string. - Only alphabets
chr within two strings. - Returns -1 if the specified substr not found. - l is the list of elements to join. 3) s.islower( )
'single'/"double" 3) index( ) - To check every alphabet is in lower, it
quotes is called
Counting no.of occurrences of str - s.index(‘substr’)
Changing case of chr of str may contain num also
string. 1) count( ) - s.index(‘substr”, begin, end) # assigning boundary 1) upper( ) 4) s.isupper( )
- s.count(‘substr’ ) - It returns the index of 1st occurrence of the provided substring. - s.upper( ) - To check every alphabet is in upper, it
- s.count(‘substr’, begin, end) - Search is from L to R - To convert all chr to uppercase may contain num also
- Returns no.of occurences of the specified - Returns Value Error if the specified substr not found 2) lower( ) 5) s.isdigits( )
string 4) rindex( ) - s.lower( ) - Only digits
- s.index(‘substr’) - To convert all chr to lowercase 6) s.istitle( )
Replacing one str with other 3) swapcase( ) - To chech whether in titlecase or not
- s.index(‘substr”, begin, end) # assigning boundary
1) replace( ) - It returns the +ve index of 1st occurrence of the provided substring in reverse - s.swapcase( ) 7) s.isspace( )
- s.replace(‘old str’, ‘new str’) order. - To interchange the case - to check if the str contain only spaces.
- Replaces the old str with new str. - Search is from R to L 4) title( )
- s.replace(‘old str’, ‘new str’, count) - Returns Value Error if the specified substr not found. - s.title( )
- Count specifies how many occurences of - To convert first letter of each word capital
old str to be replaced. 5) capitalize( )
- By replacing a newobj is been created, the - s.capitalize( )
old obj remains the same. - To change only first chr to uppercase
1) reversed( ) 2) sorted( )
- s1=reversed(s) - s1=sorted(s)
Python Inbuilt functions for str - The reversed function returns a reversed iterator object. - Return type is List
- If the given str alnum then it is sorted with digits at first and then alphabets.