Strings
Breadcrumb: /python/strings/zombiesThis page will teach you about strings in programming.
Python Strings Homework
Python Popcorn Hack
Write a program that:
-
Creates two string variables:
| name = "Your Name" | color = "Favorite Color" |
-
Prints the sentence:
Hello Alex, your favorite color is blue!
-
Then prints the same sentence, but in all uppercase letters.
# <-- Homework starts here!
# Create two string variables
name = "Meryl"
color = "blue"
# Print the sentence
sentence = f"Hello {name}, your favorite color is {color}!"
print(sentence)
# Print the same sentence in uppercase
print(sentence.upper())
Hello Meryl, your favorite color is blue!
HELLO MERYL, YOUR FAVORITE COLOR IS BLUE!
Python Strings Homework
Your program should:
-
Remove the extra spaces at the beginning and end.
-
Capitalize only the first letter of the sentence.
-
Replace the word “python” with “Python”.
-
Print the final result.
-
Also print how many characters the cleaned-up sentence has.
Hint:
Your program should be using these commands:
-
.strip()
-
.capitalize()
-
.replace()
-
len()
# Example
sentence = " rock music is loud "
cleaned = sentence.strip()
capitalized = cleaned.capitalize()
fixed = capitalized.replace("Rock", "Jazz")
print(fixed)
print(len(fixed))
Jazz music is loud
18
# <-- Homework starts here!
sentence = " python strings are powerful "
# 1. Remove spaces at beginning and end
cleaned = sentence.strip()
# 2. Capitalize the first letter
capitalized = cleaned.capitalize()
# 3. Replace "python" with "Python"
fixed = capitalized.replace("python", "Python")
# 4. Print the final result
print(fixed)
# 5. Print how many characters it has
print(len(fixed))
Python strings are powerful
27