Practice: Introduction to Strings
Q1. Print a greeting
Store the text Good morning in a variable and print it.
Goal: Create a string.
Q2. Join first and last name
Join first = "Grace" and last = "Hopper" with a space and print the result.
Goal: Use concatenation.
Q3. Draw a line
Print a line made of 20 equals signs without typing them one by one.
Goal: Use repetition.
Q4. First and last character
For word = "banana", print the first and the last character on separate lines.
Goal: Use indexing.
Q5. Count characters
Print the number of characters in the string "Hello, World!".
Goal: Use len().
Q6. Two lines with escape
Print the words Hello and World on two lines using only one print and one string.
Goal: Use escape sequences.
Q7. Quotes inside text
Print the sentence: She said "yes" to the offer. keeping the double quotes.
Goal: Handle quotes.
Q8. Number to text
Given age = 30, print the text Age: 30 by joining a string and the variable.
Goal: Convert numbers for joining.
Q9. Add two strings
What does this print?
print("10" + "20")Goal: Know that + joins strings.
Q10. Repeat text
What does this print?
print("ab" * 3)Goal: Use repetition.
Q11. Length with an escape
What does this print?
print(len("a\tb"))Goal: Count escape characters.
Q12. Raw string
What does this print?
print(r"a\nb")Goal: Know raw strings.
Q13. Negative indexes
What does this print?
s = "Python"
print(s[-1], s[-3], s[0])Goal: Use negative indexes.
Q14. Loop over a string
What does this print?
for ch in "hey":
print(ch)Goal: Iterate a string.
Q15. Membership test
What does this print?
print("cat" in "concatenate")
print("Cat" in "concatenate")Goal: Use the in operator.
Q16. Fix the TypeError
This code raises a TypeError. Fix it so it prints Total: 50.
total = 50
print("Total: " + total)Goal: Fix a type error.
Q17. Fix the immutable assignment
This code tries to change the first letter and fails. Make it print Hello.
s = "jello"
s[0] = "H"
print(s)Goal: Fix an immutability error.
Q18. Fix the quotes
This code has a syntax error because of the apostrophe. Fix it so it prints It's fine.
print('It's fine')Goal: Fix quote mistakes.
Q19. Why immutable?
Explain what it means that strings are immutable and give one example of what you can not do.
Goal: Explain immutability.
Q20. Make a banner
Write code that prints the text Menu surrounded by lines of 10 dashes above and below.
Goal: Build simple text output.