Strings and Loops
Now that we know that strings are basically lists in disguise, we can start to harness the power of loops with them.
Using a for loop
This for loop creates a variable called letter. It is used to store each character in the string as the loop goes through it, starting at the first character.
The print statement uses the letter variable and will output the string one character at a time (like a list).
Eg:
myString = "Day 38"
for letter in myString:
print(letter)
# This code outputs:
#D
#a
#y
#3
#8
# this is a comment in the code, the computer will ignore it
if statement inside the loop
This code will examine the lower case version of each character. If it's an 'a', the computer will change the font color to yellow before printing.
Outside of the loop, the last line sets the font color back to default for the next character in the loop
Eg:
myString = "Day 38"
for letter in myString:
if letter.lower() == "a":
print('\033[33m', end='') #yellow
print(letter)
print('\033[0m', end='') #back to default
# This code outputs (with a yellow 'a'):
#D
#a
#y
#3
#8
Using a list to specify search items
If the letters are in my list called vowels, they will print out in yellow.
I changed the print statement on the last line back to the default color with the ending system.
Eg:
vowels = ["a","e","i","o","u"]
myString = "Will my vowels now be yellow?"
for letter in myString:
if letter.lower() in vowels:
print('\033[33m', end='') #yellow
print(letter, end="")
print('\033[0m', end='') #back to default
Comments
Post a Comment