User Tools

Site Tools


python:stringcs

STRINGS CHEATSHEET

Python 2.4

>>> dir(string)
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']

testString = "This is a test string"
string.capwords(testString)  # 'This Is A Test String'
 
testString = "this is A test STriNg..."
testString.capitalize() # 'This is a test string'
 
testString.center(50)   # '             this is A test STriNg...             '
testString.center(50, '%') # '%%%%%%%%%%%%%this is A test STriNg...%%%%%%%%%%%%%'
 
testString.count('t') # 3
testString.count('T') # 1
 
testString.decode() # u'this is A test STriNg...'
 
testString.encode('utf-8') # 'this is A test STriNg...'
 
testString.endswith('s') # False
testString.endswith('...') # True
 
testString = "\t\tthis is A test STriNg..."
testString.expandtabs(4) # '        this is A test STriNg...'
 
# Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end].
testString = "this is A test STriNg..."
testString.find('t') # 0
testString.find('A') # 8
 
testString.isdigit           
testString.rsplit
testString.islower           
testString.rstrip
testString.isspace           
testString.split
testString.istitle           
testString.splitlines
testString.isupper           
testString.startswith
testString.join              
testString.strip
testString.ljust             
testString.swapcase
testString.lower             
testString.title
testString.lstrip            
testString.translate
testString.replace           
testString.upper
testString.index             
testString.rfind             
testString.zfill
testString.isalnum           
testString.rindex            
testString.isalpha
testString.rjust          

python/stringcs.txt · Last modified: 2013/03/16 17:41 (external edit)