Iterating through String Sequences with Loops in Python

In this activity, you will explore a number of use cases for using loops to gain more experience with iterating through string sequences and ranges of numbers.

Background

You’ve been contacted by your company’s Human Resources (HR) department to help build employee morale and excitement about FinTech. The HR department wants you to create a small Python cheerleading program that can be embedded on the homepage of the company’s intranet site. The program should loop through a string and print out a cheer using each letter in the string.

Starter file

# Create a variable named cheer and give it a word to cheer (i.e. Python or FinTech)

# Below strings can be used to add fun
cheer_symbol = "*\O/*"
cheer_symbol_2 = "ヘ( ^o^)ノ\(^_^ )"

# Loop through string and print each letter with a cheer

# Print excitement to screen
print("\nWhat does that spell?!")
print(cheer + "!\nWoohoo! Go " + cheer + "!")
print(cheer_symbol * 3)
print(cheer_symbol_2)

Instructions

  1. Create a variable named cheer that holds a single word as a string.
  2. Use a for loop to loop through each letter in the word.
  3. Print out a cheer message.

Hint

Output for this activity should resemble the following:

Solution

# Create a variable named cheer
cheer = "Python"

# Below strings can be used to add fun
cheer_symbol = "*\O/*"
cheer_symbol_2 = "ヘ( ^o^)ノ\(^_^ )"

# Loop through string
for x in cheer:
    #Print each letter with a cheer
    print("Give me a " + x + "!")
    print(x + "!")

# Print excitement to screen
print("\nWhat does that spell?!")
print(cheer + "!\nWoohoo! Go " + cheer + "!")
print(cheer_symbol * 3)
print(cheer_symbol_2)

We will be happy to hear your thoughts

Leave a reply