.lower()
, .upper()
,.capitalize()
"funKY tOwn".capitalize()
'Funky town'
"funky tOwn".lower()
'funky town'
.split([sep [,maxsplit]])
"funKY tOwn".split()
['funKY', 'tOwn']
"funKY tOwn".capitalize().split()
['Funky', 'town']
[x.capitalize() for x in "funKY tOwn".split()]
['Funky', 'Town']
"I want to take you to, funKY tOwn".split("u")
['I want to take yo', ' to, f', 'nKY tOwn']
"I want to take you to, funKY tOwn".split("you")
['I want to take ', ' to, funKY tOwn']
.strip()
, .join()
, .replace()
csv_string = 'Dog,Cat,Spam,Defenestrate,1, 3.1415 \n\t'
csv_string.strip()
'Dog,Cat,Spam,Defenestrate,1, 3.1415'
clean_list = [x.strip() for x in csv_string.split(",")]
print clean_list
['Dog', 'Cat', 'Spam', 'Defenestrate', '1', '3.1415']
.join()
allows you to glue a list of strings together with a certain string
print ",".join(clean_list)
Dog,Cat,Spam,Defenestrate,1,3.1415
print "\t".join(clean_list)
Dog Cat Spam Defenestrate 1 3.1415
.replace()
strings in strings
csv_string = 'Dog,Cat,Spam,Defenestrate,1, 3.1415 \n\t'
alt_csv = csv_string.strip().replace(' ','')
print alt_csv
Dog,Cat,Spam,Defenestrate,1,3.1415
print csv_string.strip().replace(' ','').replace(',','\t')
Dog Cat Spam Defenestrate 1 3.1415
.find()
¶incredibly useful searching, returning the index of the search
s = 'My Funny Valentine'
s.find("y")
1
s.find("y",2)
7
s[s.find("Funny"):]
'Funny Valentine'
s.find("z")
-1
ss = [s,"Argentine","American","Quarentine"]
for thestring in ss:
if thestring.find("tine") != -1:
print "'" + str(thestring) + "' contains 'tine'."
'My Funny Valentine' contains 'tine'. 'Argentine' contains 'tine'. 'Quarentine' contains 'tine'.
string
module¶exposes useful variables and functions
import string
string.swapcase("fUNKY tOWN")
'Funky Town'
print string.ascii_letters
print string.digits
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
[back]
'on {0}, I feel {1}'.format("saturday","groovy")
'on saturday, I feel groovy'
'on {}, I feel {}'.format("saturday","groovy")
'on saturday, I feel groovy'
'on {0}, I feel {1}'.format(["saturday","groovy"])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /Users/jbloom/Classes/python-bootcamp/DataFiles_and_Notebooks/06_AdvancedStrings/<ipython-input-24-37beb7743cdb> in <module>() ----> 1 'on {0}, I feel {1}'.format(["saturday","groovy"]) IndexError: tuple index out of range
'on {0}, I feel {0}'.format(["saturday","groovy"])
"on ['saturday', 'groovy'], I feel ['saturday', 'groovy']"
'on {0}, I feel {0}'.format("saturday","groovy")
'on saturday, I feel saturday'
you can assign by argument position or by name
'{desire} to {place}'.format(desire='Fly me',\
place='The Moon')
'Fly me to The Moon'
'{desire} to {place} or else I wont visit {place}.'.format( \
desire='Fly me',place='The Moon')
'Fly me to The Moon or else I wont visit The Moon.'
f = {"desire": "I want to take you", "place": "funky town"}
'{desire} to {place}'.format(**f)
'I want to take you to funky town'