Bird
Raised Fist0
Pythonprogramming~20 mins

String values and text handling in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this string slicing code?
Consider the following Python code that slices a string. What will be printed?
Python
text = "Programming"
print(text[3:8])
A"gramm"
B"gram"
C"grammi"
D"ogram"
Attempts:
2 left
💡 Hint
Remember that string slicing includes the start index but excludes the end index.
Predict Output
intermediate
2:00remaining
What does this code print with string methods?
What is the output of this Python code that uses string methods?
Python
s = "  Hello World  "
print(s.strip().lower().replace(' ', '_'))
A"hello world"
B" hello_world "
C"hello_world"
D"Hello_World"
Attempts:
2 left
💡 Hint
strip() removes spaces at both ends, lower() makes all letters lowercase, replace() changes spaces inside the string.
Predict Output
advanced
2:00remaining
What is the output of this multiline string and escape sequence code?
What will this Python code print?
Python
text = '''Line1\nLine2\tTabbed'''
print(text)
ALine1\nLine2\tTabbed
B
Line1
Line2	Tabbed
C
Line1
Line2\tTabbed
DLine1\nLine2 Tabbed
Attempts:
2 left
💡 Hint
Triple quotes preserve backslashes as literal characters unless raw strings are used.
Predict Output
advanced
2:00remaining
What is the output of this f-string with expressions?
What will this Python code print?
Python
name = "Alice"
age = 30
print(f"Name: {name.upper()}, Age next year: {age + 1}")
AName: ALICE, Age next year: 31
BName: alice, Age next year: 31
CName: ALICE, Age next year: 30
DName: Alice, Age next year: 31
Attempts:
2 left
💡 Hint
f-strings evaluate expressions inside curly braces before printing.
Predict Output
expert
3:00remaining
What is the output of this complex string manipulation code?
What will this Python code print?
Python
s = "abracadabra"
result = ''.join([c.upper() if i % 2 == 0 else c for i, c in enumerate(s)])
print(result)
AaBrAcAdAbRa
Babracadabra
CABRACADABRA
DAbRaCaDaBrA
Attempts:
2 left
💡 Hint
The code changes characters at even positions to uppercase and leaves odd positions unchanged.

Practice

(1/5)
1. What is the correct way to create a string in Python?
easy
A. Using quotes like "Hello" or 'Hello'
B. Using parentheses like (Hello)
C. Using curly braces like {Hello}
D. Using square brackets like [Hello]

Solution

  1. Step 1: Understand string syntax in Python

    Strings are text and must be enclosed in quotes, either single or double.
  2. Step 2: Check each option

    Only Using quotes like "Hello" or 'Hello' uses quotes correctly. Parentheses, curly braces, and square brackets are for other data types.
  3. Final Answer:

    Using quotes like "Hello" or 'Hello' -> Option A
  4. Quick Check:

    Strings need quotes = C [OK]
Hint: Remember: strings always need quotes around text [OK]
Common Mistakes:
  • Using parentheses instead of quotes
  • Confusing brackets with quotes
  • Forgetting to close quotes
2. Which of the following is the correct syntax to create a multi-line string in Python?
easy
A. '''This is a multi-line string'''
B. "This is a multi-line string"
C. 'This is a multi-line string'
D. --This is a multi-line string--

Solution

  1. Step 1: Recall multi-line string syntax

    Python uses triple quotes (either ''' or """) to create strings that span multiple lines.
  2. Step 2: Evaluate options

    '''This is a multi-line string''' uses triple single quotes correctly. Options B and C use single or double quotes for single-line strings. --This is a multi-line string-- is invalid syntax.
  3. Final Answer:

    '''This is a multi-line string''' -> Option A
  4. Quick Check:

    Triple quotes = multi-line string [OK]
Hint: Use triple quotes for multi-line strings [OK]
Common Mistakes:
  • Using single or double quotes for multi-line text
  • Using invalid symbols like --
  • Not closing triple quotes properly
3. What will be the output of the following code?
text = "Hello World"
print(text[6:])
medium
A. Hello
B. World
C. World!
D. Hello World

Solution

  1. Step 1: Understand string slicing

    text[6:] means start from index 6 to the end of the string. Indexing starts at 0.
  2. Step 2: Identify substring from index 6

    In "Hello World", index 6 is 'W', so text[6:] is "World".
  3. Final Answer:

    World -> Option B
  4. Quick Check:

    text[6:] = World [OK]
Hint: Remember string index starts at 0; slice from index 6 to end [OK]
Common Mistakes:
  • Confusing index start point
  • Including extra characters like '!'
  • Printing entire string instead of slice
4. Find the error in this code snippet:
name = 'Alice
print(name)
medium
A. Incorrect use of newline character
B. print() function is misspelled
C. Missing closing quote for the string
D. Variable name is invalid

Solution

  1. Step 1: Check string syntax

    The string starts with a single quote but does not have a closing quote before the newline.
  2. Step 2: Identify syntax error

    Because the closing quote is missing, Python will raise a syntax error.
  3. Final Answer:

    Missing closing quote for the string -> Option C
  4. Quick Check:

    Unclosed string causes syntax error [OK]
Hint: Always close your quotes before newline or end [OK]
Common Mistakes:
  • Forgetting to close quotes
  • Thinking newline character causes error
  • Assuming print is misspelled
5. You have a list of words: words = ['apple', 'banana', 'cherry']. Which code correctly joins them into a single string separated by commas?
hard
A. result = ','.join([words])
B. result = words.join(',')
C. result = ','.join('words')
D. result = ','.join(words)

Solution

  1. Step 1: Understand join() method usage

    The join() method is called on the string separator and takes an iterable of strings as argument.
  2. Step 2: Check each option

    result = ','.join(words) correctly calls join on the comma string with the list words. Others misuse join or pass wrong arguments.
  3. Final Answer:

    result = ','.join(words) -> Option D
  4. Quick Check:

    Separator.join(list) joins list items [OK]
Hint: Call join on separator string, pass list to join() [OK]
Common Mistakes:
  • Calling join on the list instead of the string
  • Passing string 'words' instead of list variable
  • Passing list inside another list