Strings
Codecademy python working with strings
Thread Shed
I got a blob of text shown here above. I'll use this snippet instead of the entire blob.
daily_sales = \
"""Edith Mcbride ;,;$1.21 ;,; white ;,;
09/15/17 ,Herbert Tran ;,; $7.29;,;
white&blue;,; 09/15/17 ,Paul Clarke ;,;$12.52
;,; white&blue ;,; 09/15/17"""
1. Replace
First step, replace ;,;
for +
using string.replace(old, new)
daily_sales_replaced = daily_sales.replace(";,;", "+")
Edith Mcbride +$1.21 + white +
09/15/17 ,Herbert Tran + $7.29+
white&blue+ 09/15/17 ,Paul Clarke +$12.52
+ white&blue + 09/15/17
2. Split
Then we split the strings at the ,
into a list using string.split(delimiter)
daily_transactions = daily_sales_replaced.split(",")
['Edith Mcbride +$1.21 + white + \n09/15/17 ', 'Herbert Tran + $7.29+ \nwhite&blue+ 09/15/17 ', 'Paul Clarke +$12.52 \n+ white&blue + 09/15/17']
3. Split attributes
Now split attributes of each transaction and remove the +
from step 1
# create empty list
daily_transactions_split = []
# Iterate, append and split a transaction.
for transaction in daily_transactions:
daily_transactions_split.append(transaction.split("+"))
[['Edith Mcbride ', '$1.21 ', ' white ', ' \n09/15/17 '], ['Herbert Tran ', ' $7.29', ' \nwhite&blue', ' 09/15/17 '], ['Paul Clarke ', '$12.52 \n', ' white&blue ', ' 09/15/17']]
4. Replace and strip
Using 2 for loops replace the \n
for a ""
and strip
whitespaces. First loop iterates over daily_transactions_split. The second for loop is iterating over the individual datapoints as we're not cleaning transactions.
# Empty list
transactions_clean = []
for transaction in daily_transactions_split:
trans_clean = []
for item in transaction:
trans_clean.append(item.replace("\n", "").strip())
transactions_clean.append(trans_clean)
Now it starting to look clean
[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17'], ['Paul Clarke', '$12.52', 'white&blue', '09/15/17']]
[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran', '$7.29', 'white&blue', '09/15/17'], ['Paul Clarke', '$12.52', 'white&blue', '09/15/17']]
5. Arrange data in lists
# Create lists
customers = []
sales = []
thread_sold = []
# First item in transaction goes to index[0] and so on.
for transaction in transactions_clean:
customers.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])
['Edith Mcbride', 'Herbert Tran', 'Paul Clarke']
['$1.21', '$7.29', '$12.52']
['white', 'white&blue', 'white&blue']
6. Calculate total sales
Converting string to number using float
and strip to remove the $
total_sales = 0
for sale in sales:
total_sales += float(sale.strip("$"))
21.02
7. Remove multiple colors
Using split
we remove the &
and append
it in new list
thread_sold_split = []
for sale in thread_sold:
for color in sale.split("&"):
thread_sold_split.append(color)
['white', 'white', 'blue', 'white', 'blue']
8. Create function
Make function to count the number of colors.
# It checks colors in thread_sold_split and if machtes input like "white" it will add 1 to total color.
def color_count(color):
total_color = 0
for thread_color in thread_sold_split:
if color == thread_color:
total_color += 1
return total_color
# Count color white
print(color_count("white"))
# Will result in:
3
9. Print using format
Printing a string showing how many threads sold using format
.
# list with colors
colors = ['red', 'yellow', 'green', 'white', 'black', 'blue', 'purple']
# loop which uses function with format to print amount and color, at {0} and {1}
for color in colors:
print("There were {0} {1} threads sold today.".format(color_count(color), color))
There were 0 red threads sold today.
There were 0 yellow threads sold today.
There were 0 green threads sold today.
There were 3 white threads sold today.
There were 0 black threads sold today.
There were 2 blue threads sold today.
There were 0 purple threads sold today.
Last updated
Was this helpful?