Python Strings – Part 4

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

So far, you’ve learnt:

  • Part 1: The basics — quotes, printing, joining, length, and indexes.
  • Part 2: Escapes, multi-line strings, and slicing.
  • Part 3: String methods for cleaning and transforming text.

Now it’s time for Part 4: String Formatting.

Formatting means combining variables and strings in neat, readable ways. Instead of messy concatenation with +, formatting lets you create sentences that look natural while still including dynamic values like names, numbers, and results from calculations.

The problem with +

At first, you might try joining text with +. This works fine if everything is already text:

But problems appear when you mix in numbers (like ages, scores, or prices).

Why? Because Python won’t automatically convert the number 12 into a string.
You can fix it by converting manually with str(age):

This works, but it’s clunky and hard to read. Luckily, Python gives us better ways.

F-strings (the easy way)

F-strings are the most modern and beginner-friendly solution. The idea is simple:

  • Put an f right before the quotes.
  • Drop variables directly into the string using { }.

No need to convert numbers or break up the sentence — Python handles it all.

You can even put expressions inside the curly braces:

This makes your output short, clean, and very readable.

Try this

  1. Make variables for your name and favourite colour.
  2. Print a sentence like: "My name is ___ and my favourite colour is ___."
  3. Add a variable for your age and include it in the same sentence.

Using .format()

Before f-strings were introduced, Python used the .format() method. It’s still common in older tutorials and codebases, so it’s worth knowing.

The { } placeholders are replaced by the values you give to .format(), in the same order.

You can also reorder or repeat values using index numbers inside the braces:

This flexibility is why .format() was popular — but for new learners, f-strings are simpler.

Try this

  1. Print "I like apples and oranges" using .format().
  2. Swap the order so it prints "I like oranges and apples".

Old-style % formatting

Python also has an older style that uses % signs. You may see it in very old code, but it’s mostly replaced by .format() and f-strings.

Here:

  • %s is for strings
  • %d is for integers (whole numbers)
  • %f is for floating point numbers (decimals)

This style can look confusing, so don’t worry if it feels odd. Just know it exists.

Formatting numbers

Formatting isn’t only for inserting variables — it can also control how numbers appear.

With f-strings you can:

  • Round numbers to a set number of decimal places.
  • Add commas for large numbers.

Examples:

This is very useful for things like money, scores, and reports, where presentation matters.

Try this

  1. Make pi = 3.14159265. Print it rounded to 2 decimal places.
  2. Make big = 1234567890. Print it with commas.

Putting it all together

Here’s a short program that uses f-strings to format text and numbers neatly:

Output:

See how much easier it is to read and understand compared to using + everywhere.

Try this

Write a program that:

  1. Asks the user for their name and age.
  2. Prints: "Hello NAME, next year you will be AGE+1." using an f-string.

Summary

  • + works for joining text but is clumsy with numbers.
  • F-strings are the modern, easiest way to combine variables and text.
  • .format() is older but still useful to recognise.
  • % formatting is very old, mostly replaced now.
  • F-strings can also format numbers (rounding, commas, etc.).

✅ With string formatting, you can now build clear, professional-looking messages, reports, and outputs in your Python programs.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Python Numbers

Learn Python numbers: integers and floats, arithmetic operations, conversions, rounding, precision issues, and practical exercises for beginners…

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

Python Strings – Part 3

Learn Python string methods: change case, remove spaces, replace text, split and join words, and check content…

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

Python Strings – Part 2

Explore Python strings further: escape characters, multi-line strings, and slicing to extract parts of text, with simple…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

Python Syntax

Learn Python syntax basics with clear examples and beginner-friendly practice exercises.

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.

Python Variables

Python Variables

Learn Python variables with simple explanations, real-world analogies, practice tasks to build confidence using data in programs.