Python - String Methods List


The following table summarizes the methods and call patterns for built-in string method.

S.capitalize()                                S.ljust(width [, fill]) 
S.casefold()                                  S.lower() [, fill])                      S.lstrip([chars]) 
S.count(sub [, start [, end]])                S.maketrans(x[, y[, z]]) 
S.encode([encoding [,errors]])                S.partition(sep) 
S.endswith(suffix [, start [, end]])          S.replace(old, new [, count]) 
S.expandtabs([tabsize])                       S.rfind(sub [,start [,end]]) 
S.find(sub [, start [, end]])                 S.rindex(sub [, start [, end]]) 
S.format(fmtstr, *args, **kwargs)             S.rjust(width [, fill]) 
S.index(sub [, start [, end]])                S.rpartition(sep) 
S.isalnum()                                   S.rsplit([sep[, maxsplit]]) 
S.isalpha()                                   S.rstrip([chars]) 
S.isdecimal()                                 S.split([sep [,maxsplit]]) 
S.isdigit()                                   S.splitlines([keepends]) 
S.isidentifier()                              S.startswith(prefix [, start [, end]]) 
S.islower()                                   S.strip([chars]) 
S.isnumeric()                                 S.swapcase() 
S.isprintable()                               S.title() 
S.isspace()                                   S.translate(map) 
S.istitle()                                   S.upper() 
S.isupper()                                   S.zfill(width) 


line = "This is a test!\n" 
print( line.rstrip() )
print( line.upper() )
print( line.isalpha() )
print( line.endswith('Ni!\n') )
print( line.startswith('The') )

line = "This is a Ni!\n" 

print( line )#  ww  w.jav a2s  .c  o m
print( line.find('Ni') != -1 )       # Search via method call or expression 
print( 'Ni' in line )

sub = 'Ni!\n' 
print( line.endswith(sub) )          # End test via method call or slice 
print( line[-len(sub):] == sub )