Chapter 9: String Formatting and Methods
Complete Python lesson for very beginners. Technical words are explained in simple language, and every outline topic includes a practical example, expected output, steps, and practice.
Chapter Overview
This Python tutorial chapter covers String Formatting and Methods through 15 connected topics. Work through the examples in order, check the expected output, and complete the practice after each topic.
- 9.1 f-Strings
- 9.2 format()
- 9.3 Format Specifications
- 9.4 Alignment
- 9.5 Number Formatting
- Plus 10 additional Python topics in this chapter.
9.1 f-Strings
f-Strings is part of String Formatting and Methods. In simple language, it means text stored as characters.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
score = 92
print(f"{name} scored {score}%")Expected Output
Ava scored 92%Step-by-Step Explanation
- Put f before the string.
- Place expressions inside braces.
- Python inserts their values into the text.
9.2 format()
format() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
print(f"Hello {name}")Expected Output
Hello AvaStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
9.3 Format Specifications
Format Specifications is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
print(f"Hello {name}")Expected Output
Hello AvaStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
9.4 Alignment
Alignment is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
print(f"Hello {name}")Expected Output
Hello AvaStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
9.5 Number Formatting
Number Formatting is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
print(f"Hello {name}")Expected Output
Hello AvaStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
9.6 upper() and lower()
upper() and lower() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
text = "Python"
print(text.upper())
print(text.lower())Expected Output
PYTHON
pythonStep-by-Step Explanation
- String methods return new strings.
- upper() capitalizes letters.
- lower() makes letters lowercase.
9.7 strip()
strip() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
text = " hello "
print(text.strip())Expected Output
helloStep-by-Step Explanation
- The original string contains surrounding spaces.
- strip() removes leading and trailing whitespace.
- The middle text remains.
9.8 split()
split() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
text = "red,blue,green"
print(text.split(","))Expected Output
['red', 'blue', 'green']Step-by-Step Explanation
- Choose a delimiter.
- split() breaks the string at that delimiter.
- The result is a list of strings.
9.9 join()
join() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
parts = ["2026", "09", "21"]
print("-".join(parts))Expected Output
2026-09-21Step-by-Step Explanation
- Start with strings in a list.
- Call join() on the separator.
- A single combined string is returned.
9.10 replace()
replace() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
text = "I like cats"
print(text.replace("cats", "Python"))Expected Output
I like PythonStep-by-Step Explanation
- replace() searches for matching text.
- It returns a new string with replacements.
- The original string is unchanged.
9.11 find()
find() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
text = "learn Python"
print(text.find("Python"))Expected Output
6Step-by-Step Explanation
- find() searches for a substring.
- It returns the starting index.
- If the text is not found, it returns -1.
9.12 startswith() and endswith()
startswith() and endswith() is part of String Formatting and Methods. In simple language, it means a Python idea used while learning string formatting and methods.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "report.csv"
print(name.startswith("rep"))
print(name.endswith(".csv"))Expected Output
True
TrueStep-by-Step Explanation
- startswith() checks the beginning.
- endswith() checks the ending.
- Both return boolean results.
9.13 Character Testing Methods
Character Testing Methods is part of String Formatting and Methods. In simple language, it means a function associated with an object or class.
It makes programs easier to trust, diagnose, change, and maintain as they become larger. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import unittest
def add(a, b): return a + b
class AddTest(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
result = unittest.TextTestRunner(verbosity=0).run(unittest.defaultTestLoader.loadTestsFromTestCase(AddTest))
print(result.wasSuccessful())Expected Output
TrueStep-by-Step Explanation
- Define behavior to test.
- Create a TestCase method whose name begins with test.
- Run the test and check that it succeeds.
9.14 Encoding and Decoding
Encoding and Decoding is part of String Formatting and Methods. In simple language, it means rules for converting text characters to and from bytes.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
name = "Ava"
print(f"Hello {name}")Expected Output
Hello AvaStep-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
9.15 Practical Text Processing
Practical Text Processing is part of String Formatting and Methods. In simple language, it means a running program with its own process resources.
It is one building block of string formatting and methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
from multiprocessing import cpu_count
print(cpu_count() >= 1)Expected Output
TrueStep-by-Step Explanation
- multiprocessing provides process-based parallelism tools.
- cpu_count() reports available logical CPUs as seen by Python.
- Process-based work has communication and startup costs.
Common Beginner Mistakes
- Copying code without predicting what each line does.
- Ignoring the first useful error message or traceback location.
- Mixing tabs/spaces or changing indentation accidentally.
- Using data of the wrong type for an operation.
- Trying to learn many advanced variations before mastering one small working example.
Chapter Practice
- Choose three topics from this chapter and re-type their examples without copying and pasting.
- For each example, change one input and predict the output first.
- Explain five technical terms from this chapter in your own beginner-friendly words.
- Create one small program that combines at least two chapter topics.
- Keep notes about errors you made and what fixed them.
Mini Project / Challenge
Create a small Python exercise that combines at least three ideas from String Formatting and Methods. Start with a tiny working version, test it, then improve it one step at a time.
- Choose three topics from this chapter.
- Write or adapt a small Python example using those topics.
- Predict the output before running the code.
- Test at least one different input.
- Write two sentences explaining what the program does and what you learned.
20 Questions & Answers
1. What is the main goal of Chapter 9?
The goal is to understand string formatting and methods through small explanations, examples, and practice.
2. Should I memorize every command or method?
No. Understand the pattern, practice the common form, and learn how to read documentation when you need exact details.
3. Why are the examples small?
Small examples isolate one idea at a time, which makes errors easier to understand and fix.
4. What should I do when an example gives an error?
Read the last part of the traceback, check spelling and indentation, confirm the required package or file exists, and compare the input types with what the operation expects.
5. Why should I predict output before running code?
Prediction forces you to reason about the program instead of only copying it.
6. What should I remember about f-Strings?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
7. What should I remember about format()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
8. What should I remember about Format Specifications?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
9. What should I remember about Alignment?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
10. What should I remember about Number Formatting?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
11. What should I remember about upper() and lower()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
12. What should I remember about strip()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
13. What should I remember about split()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
14. What should I remember about join()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
15. What should I remember about replace()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
16. What should I remember about find()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
17. What should I remember about startswith() and endswith()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
18. What should I remember about Character Testing Methods?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
19. What should I remember about Encoding and Decoding?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
20. What should I remember about Practical Text Processing?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.