Practice: Introduction to Strings

« Read the chapter

Q1. Print a greeting

BeginnerCoding

Store the text Good morning in a variable and print it.

Goal: Create a string.

Q2. Join first and last name

BeginnerCoding

Join first = "Grace" and last = "Hopper" with a space and print the result.

Goal: Use concatenation.

Q3. Draw a line

BeginnerCoding

Print a line made of 20 equals signs without typing them one by one.

Goal: Use repetition.

Q4. First and last character

BeginnerCoding

For word = "banana", print the first and the last character on separate lines.

Goal: Use indexing.

Q5. Count characters

BeginnerCoding

Print the number of characters in the string "Hello, World!".

Goal: Use len().

Q6. Two lines with escape

IntermediateCoding

Print the words Hello and World on two lines using only one print and one string.

Goal: Use escape sequences.

Q7. Quotes inside text

IntermediateCoding

Print the sentence: She said "yes" to the offer. keeping the double quotes.

Goal: Handle quotes.

Q8. Number to text

IntermediateCoding

Given age = 30, print the text Age: 30 by joining a string and the variable.

Goal: Convert numbers for joining.

Q9. Add two strings

BeginnerOutput

What does this print?

Code
print("10" + "20")

Goal: Know that + joins strings.

Q10. Repeat text

BeginnerOutput

What does this print?

Code
print("ab" * 3)

Goal: Use repetition.

Q11. Length with an escape

IntermediateOutput

What does this print?

Code
print(len("a\tb"))

Goal: Count escape characters.

Q12. Raw string

IntermediateOutput

What does this print?

Code
print(r"a\nb")

Goal: Know raw strings.

Q13. Negative indexes

BeginnerOutput

What does this print?

Code
s = "Python"
print(s[-1], s[-3], s[0])

Goal: Use negative indexes.

Q14. Loop over a string

IntermediateOutput

What does this print?

Code
for ch in "hey":
    print(ch)

Goal: Iterate a string.

Q15. Membership test

IntermediateOutput

What does this print?

Code
print("cat" in "concatenate")
print("Cat" in "concatenate")

Goal: Use the in operator.

Q16. Fix the TypeError

BeginnerDebugging

This code raises a TypeError. Fix it so it prints Total: 50.

Code
total = 50
print("Total: " + total)

Goal: Fix a type error.

Q17. Fix the immutable assignment

IntermediateDebugging

This code tries to change the first letter and fails. Make it print Hello.

Code
s = "jello"
s[0] = "H"
print(s)

Goal: Fix an immutability error.

Q18. Fix the quotes

BeginnerDebugging

This code has a syntax error because of the apostrophe. Fix it so it prints It's fine.

Code
print('It's fine')

Goal: Fix quote mistakes.

Q19. Why immutable?

IntermediateConceptual

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

IntermediateApplication

Write code that prints the text Menu surrounded by lines of 10 dashes above and below.

Goal: Build simple text output.