* * * * * Python "STRING" Methods & Functions Cheat Sheet * * * * *
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) [Link](‘substr’)
- Compares based on Unicode values - [Link](‘substr’) - [Link]( ) 2) [Link](‘substr’)
- Compares first different chr - [Link](‘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 - [Link](‘substr”) Joining the string Cheaking type of chr in a str
1)lstrip( ) # removes left side spaces - [Link](‘substr’, begin, end) # assigning boundary 1) join( ) 1) [Link]( ) # (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) [Link]( )
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) [Link]( )
'single'/"double" 3) index( ) - To check every alphabet is in lower, it
quotes is called
Counting [Link] occurrences of str - [Link](‘substr’)
Changing case of chr of str may contain num also
string. 1) count( ) - [Link](‘substr”, begin, end) # assigning boundary 1) upper( ) 4) [Link]( )
- [Link](‘substr’ ) - It returns the index of 1st occurrence of the provided substring. - [Link]( ) - To check every alphabet is in upper, it
- [Link](‘substr’, begin, end) - Search is from L to R - To convert all chr to uppercase may contain num also
- Returns [Link] occurences of the specified - Returns Value Error if the specified substr not found 2) lower( ) 5) [Link]( )
string 4) rindex( ) - [Link]( ) - Only digits
- [Link](‘substr’) - To convert all chr to lowercase 6) [Link]( )
Replacing one str with other 3) swapcase( ) - To chech whether in titlecase or not
- [Link](‘substr”, begin, end) # assigning boundary
1) replace( ) - It returns the +ve index of 1st occurrence of the provided substring in reverse - [Link]( ) 7) [Link]( )
- [Link](‘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( )
- [Link](‘old str’, ‘new str’, count) - Returns Value Error if the specified substr not found. - [Link]( )
- 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 - [Link]( )
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.