Beginning Python: More Tricks with Lists, While Loops
2025-10-10
if = 1 xfor = multiple times.strip(), .lower()words:for loop that goes through each word. Inside the loop:
.strip() and .lower() to clean the wordcleanedcleaned in the loop..append().append()But how to maintain these words in a list?
First, more straightforward (but less efficient) way:
words = [" Cat\n", "dog", " BIRD ", "fish\n", "LION", "tiger ", "bear", "OWL\n"]
cleaned_words = []
for word in words:
cleaned = word.strip().lower()
cleaned_words.append(cleaned)
print(cleaned_words).append() method.append():len() command:.replace()items list.replace().replace()items = ["color", "flavour", "theater", "center", "analyze", "organize", "favorite", "neighbor", "honor", "catalog", "honour", "flavor", "analyse", "flavor", "traveler"]
down_with_brits = []
for word in items:
if word.endswith("our"):
word = word.replace("our", "or")
down_with_brits.append(word)
print(down_with_brits).endswith() method - there’s also .startswith().replace() Activityif block to have words ending in “-yse” respelled as “-yze”.split()getty = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."
getty.split()split() separates according to whitespace..split()getty = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."
getty_1 = getty.split(",")
getty_2 = getty.split(".")
getty_3 = getty.split("e")
print(getty_1, "\n", getty_2, "\n", getty_3)re.split()regex.py file):re.split()import re
text = "Hello, world! This... is a test."
words = re.split(r"[\s\W]+", text)
words = [w for w in words if w]
print(words)\s = whitespace\W = non-word characters (punctuation, symbols, etc.)+ = one or more precedingre.split()alice.txt (found at https://www.gutenberg.org/cache/epub/11/pg11.txt)alice.txt in Python using the following code:re.split() create a new list called alice_list, containing all words, minus whitespace and non-word charactersprint(alice_text[0:100])while Loopswhile Loopsif statements, where something happens if a condition is met:while Loopswhile loops, there’s also a logical conditionFalsewhile Loopswhile loops! Code (like below) will run ad infinitum and could crash your computer:x = 0
while x < 10:
print(x, "is less than 10.")
x = x - 1
while Loopsfor loopswhile loops, like:items = ["color", "flavour", "theater", "center", "analyze", "organize", "favorite", "neighbor", "honor", "catalog", "honour", "flavor", "analyse", "flavor", "traveler"]
down_with_brits = []
for word in items:
if word.endswith("our"):
word = word.replace("our", "or")
elif word.endswith("yse"):
word = word.replace("yse", "yze")
down_with_brits.append(word)
print(down_with_brits)
import re
with open("alice.txt", "r", encoding="utf-8") as f:
alice_text = f.read()
alice_list = re.split(r"[\s\W]+", alice_text)
alice_list = [w for w in alice_list if w]
print(alice_list[0:100])