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.
Need a hint?
Join two strings with +, putting a " " space string between them; len() counts every character including the space.
Show the solution
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.
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
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.
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
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}")
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-17and print it with every-replaced by/.
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
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).
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
Problem-3: Is It A Number?
Write a program that prints whether "2026" is all digits and whether "20a6" is all digits.
Need a hint?
.isdigit() returns True only when every character is a digit, so any letter makes it False.
Show the solution
Problem-4: Ends and Position.
Write a program that:
- Task-1: for a filename
data.csv, print whether it starts withdata. - Task-2: for the same filename, print whether it ends with
.csv. - Task-3: print the position of
"ss"in"mississippi", found two ways.
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
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"))
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.
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
Problem-2 (Write a Program):
Write a program that asks the user for a word and prints its first letter and its last letter.
Need a hint?
input() gives a string you can index; [0] is the first character and [-1] is the last.
Show the solution
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).
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
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.
Practice: Variables and Data Types Next: Practice: Operators Booleans and Conditionals