.split()
¶question = 'Do you know the muffin man?'
print(question.split())
statement = "It is possible that I might, under other circumstances, have something to say, but right now I don't"
print(statement.split(','))
NOTES
','
is the delimiterThe string you use as the separator is called the delimiter
text = "This string has\nvery unusual \n whitespace!"
print(text)
text.split()
text.split(' ')
text.split('\n')
NOTES
split
will combine all whitespace into a single delimiter.join()
¶' '.join(['a','bunch','of','words'])
','.join(['45', '670', '324'])
sentences = [
'This is the first line.',
'This is the second line.',
'And here is another line for good measure.'
]
print(sentences)
text = '\n'.join(sentences)
print(text)
' and '.join(['apples','oranges','bananas','pears'])
NOTES
' '.join('Cougars')
NOTES
' '.join([1, 2, 3, 4])
NOTES
random
¶NOTES
random.choice()
¶import random
fruits = ['pear','mango','banana','strawberry','kumquat']
selection = random.choice(fruits)
print(selection)
random.randint()
¶import random
print(random.randint(1, 10))
NOTES
random.randint()
can return the boundary numbersrandom.random()
¶print(random.random())
If you need a random float between 0 and 100, just scale the random number:
print(random.random() * 100)
random.sample()
¶name = 'Washington'
print(random.sample(name, 2))
print(random.sample(name, 5))
print(random.sample(name, len(name)))
random.sample
can be used to get a random sample from a collection.
If you sample all the items in the collection, you essentially get a shuffled version of the data.
%%file apples.py
def apples(size):
result = []
while len(result) < size:
if random.random() < 0.1:
result.append('bananas!')
else:
result.append('apples')
return result
size = int(sys.argv[1])
fruit = apples(size)
print(' '.join(fruit))
apples.py
¶Randomly inject "umm" into a given sentence at a frequency of about 20%.
umm.py
¶NOTES
import random
import sys
def ummify(words):
"""Randomly inject 'umm' into the given text at a frequence of 20%"""
words = text.split(' ')
result = []
for word in words:
if random.random() < 0.2:
result.append('umm')
result.append(word)
return ' '.join(result)
text = sys.argv[1]
print(ummify(text))
python umm.py "So, I've been meaning to ask, will you go out on a date with me?"
def has_digit(text):
"""Returns true if any character in `text` is a digit"""
for char in text:
if char.isdigit():
return True
return False
NOTES
for
loop never gets to finish. That's OK.return False
is correct because if we didn't return True
, we never found a digit.has_digit('abcdefg123')
has_digit('a few words')
has_byu
¶def has_byu(lines):
"""Returns True if any of the lines contain the text 'BYU'
lines = ['abc', 'byu', '123'] => True
lines = ['123', 'abc'] => False
"""
for line in lines:
if 'BYU' in line or 'byu' in line:
return True
return False
def has_byu(lines):
"""Returns True if any of the lines contain the text 'BYU'"""
for line in lines:
if 'BYU' in line:
return True
return False
has_byu(['Yesterday I hiked Y mountain','I saw a squirrel','The squirrel ran away'])
has_byu(['I need to buy books','The BYU bookstore has chocolate-covered cinnammon bears','I have a plan!'])
all_digits
¶def all_digits(text):
"""Returns True if all of the characters are digits"""
for char in text:
if not char.isdigit():
return False
return True
def all_digits(text):
"""Returns True if all of the characters are digits"""
for c in text:
if not c.isdigit():
return False
return True
all_digits('12345')
all_digits('123e32')
.split()
and .join()
random