Skip to content

Practice: Strings and f-strings

View practice code on GitHub Get the Video Course

From the lesson

Practice problems for Section 03 Concepts. Build each yourself first, then expand the solution to check.

Four practice problems, one per concept step; the numbers line up (Practice-01 goes with Step-01). Build each yourself, then check python-practice/solutions. Some problems end with a short input() "Write a Program" exercise.

Step-01: Making Strings

Concepts: strings, joining with +, len(), multiline strings.

Problem-1: Full Name.

Write a program that joins a first name (Srihan) and a last name (Reddy) into a full name with a space between them, prints the full name, and prints how many characters it has.

Expected output
Srihan Reddy
12
Need a hint?

Join two strings with +, putting a " " space string between them; len() counts every character including the space.

Show the solution
s03_practice01_sol_making_strings.py
first_name = "Srihan"
last_name = "Reddy"
full_name = first_name + " " + last_name
print(full_name)

print(len(full_name))

View the full solution file on GitHub

Problem-2: Multiline bio.

Write a program that prints a three-line bio from a single multiline string: Srihan Reddy, then Python Learner, then Chennai.

Expected output
Srihan Reddy
Python Learner
Chennai
Need a hint?

Triple quotes let one string span several lines; each line break you type becomes a newline in the output.

Show the solution
s03_practice01_sol_making_strings.py
bio = """Srihan Reddy
Python Learner
Chennai"""
print(bio)

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-02: f-strings

Concepts: f-strings, math inside braces, format specs.

Write a program that, using a name (Srihan), an age (27), and a price (2499.95) with f-strings:

  • Task-1: print Hi Srihan, you are 27.
  • Task-2: print Next year you will be 28 (the age plus 1).
  • Task-3: print the price formatted with 2 decimals and a thousands separator as Price: 2,499.95.
Expected output
Hi Srihan, you are 27
Next year you will be 28
Price: 2,499.95
Need a hint?

Inside an f-string, {} drops a value into the text and you can do math like {age + 1}; {price:,.2f} adds thousands commas and 2 decimals.

Show the solution
s03_practice02_sol_fstrings.py
name = "Srihan"
age = 27
price = 2499.95
# Task-1: print greeting with name and age
print(f"Hi {name}, you are {age}")

# Task-2: print next year's age
print(f"Next year you will be {age + 1}")

# Task-3: print price with 2 decimals and thousands separator
print(f"Price: {price:,.2f}")

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-03: String Methods

Concepts: strip, upper, replace, input(), f-strings, len(), startswith/endswith, find/index.

Warning

String methods like .strip(), .upper(), and .replace() do not change the original string. Strings are immutable, so each method returns a NEW string. Print the returned value (or store it in a new variable); the original variable is unchanged.

Problem-1: Clean Up Text

Write a program that:

  • Task-1: take a greeting " Hello World " (note the extra spaces) and print it with the surrounding spaces removed and in UPPER CASE.
  • Task-2: take a date 2026-06-17 and print it with every - replaced by /.
Expected output
HELLO WORLD
2026/06/17
Need a hint?

.strip() removes only leading and trailing spaces; chain .strip().upper() so each method works on the previous result. .replace(old, new) swaps every match.

Show the solution
s03_practice03_sol_string_methods.py
greeting = "  Hello World  "
# Problem-1 Task-1: strip spaces and uppercase the greeting
print(greeting.strip().upper())
date = "2026-06-17"
# Problem-1 Task-2: replace dashes with slashes
print(date.replace("-", "/"))

View the full solution file on GitHub

Problem-2 (Write a Program): Name Tag.

Write a program that asks the user for a full name, then prints NAME TAG: <NAME> with the name in UPPER CASE and Characters: <count> with the number of characters in the name (spaces count).

Example run
Enter your full name: Kalyan Reddy
NAME TAG: KALYAN REDDY
Characters: 12
Need a hint?

input() always returns a string, so .upper() and len() work on it directly; len() counts the typed space too.

Show the solution
s03_practice03_sol_string_methods.py
full_name = input("Enter your full name: ")
print(f"NAME TAG: {full_name.upper()}")
print(f"Characters: {len(full_name)}")

View the full solution file on GitHub

Problem-3: Is It A Number?

Write a program that prints whether "2026" is all digits and whether "20a6" is all digits.

Expected output
True
False
Need a hint?

.isdigit() returns True only when every character is a digit, so any letter makes it False.

Show the solution
s03_practice03_sol_string_methods.py
print("2026".isdigit())
print("20a6".isdigit())

View the full solution file on GitHub

Problem-4: Ends and Position.

Write a program that:

  • Task-1: for a filename data.csv, print whether it starts with data.
  • Task-2: for the same filename, print whether it ends with .csv.
  • Task-3: print the position of "ss" in "mississippi", found two ways.
Expected output
True
True
2
2
Need a hint?

.startswith() and .endswith() return booleans; .find() and .index() give the first position of a substring, differing only when it is missing.

Show the solution
s03_practice03_sol_string_methods.py
filename = "data.csv"
# Problem-4 Task-1: check if filename starts with "data"
print(filename.startswith("data"))
# Problem-4 Task-2: check if filename ends with ".csv"
print(filename.endswith(".csv"))
# Problem-4 Task-3: find position of "ss" two ways
print("mississippi".find("ss"))
print("mississippi".index("ss"))

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Step-04: Indexing and Slicing

Concepts: indexing, slicing, input(), step/reverse slices, immutability.

Problem-1

Write a program that, for the word Rocket:

  • Task-1: print its first letter.
  • Task-2: print its first 3 letters.
  • Task-3: print its last 3 letters.
Expected output
R
Roc
ket
Need a hint?

word[0] indexes one character; word[0:3] slices a range; a negative start like word[-3:] counts from the end.

Show the solution
s03_practice04_sol_indexing_slicing.py
word = "Rocket"
# Problem-1 Task-1: print the first letter
print(word[0])
# Problem-1 Task-2: print the first 3 letters
print(word[0:3])
# Problem-1 Task-3: print the last 3 letters
print(word[-3:])

View the full solution file on GitHub

Problem-2 (Write a Program):

Write a program that asks the user for a word and prints its first letter and its last letter.

Example run
Type a word: Mazda
M
a
Need a hint?

input() gives a string you can index; [0] is the first character and [-1] is the last.

Show the solution
s03_practice04_sol_indexing_slicing.py
typed = input("Type a word: ")
print(typed[0])
print(typed[-1])

View the full solution file on GitHub

Problem-3: Step and Reverse.

Write a program that, for the word Rocket:

  • Task-1: print every second character.
  • Task-2: print the word reversed.
  • Task-3: print the word with its first letter changed to J (remember strings are immutable, so build a new string).
Expected output
Rce
tekcoR
Jocket
Need a hint?

word[::2] steps by 2 and word[::-1] reverses; strings are immutable, so build a new one with "J" + word[1:].

Show the solution
s03_practice04_sol_indexing_slicing.py
word = "Rocket"
# Problem-3 Task-1: print every second character
print(word[::2])
# Problem-3 Task-2: print the word reversed
print(word[::-1])
# Problem-3 Task-3: build word with first letter changed to J
print("J" + word[1:])

View the full solution file on GitHub

Pages for this step: Concept | Practice | Workshop

Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions

Finished this section?

Check your work against the solutions above, then head back to the Section 03 lesson, or try the Section 03 workshop for more reps.

Prefer to learn by watching?

The full video course teaches every concept on screen and builds the projects with you, step by step.

Get the Video Course

Practice: Variables and Data Types Next: Practice: Operators Booleans and Conditionals