Loading...

Python String Format

The scenario

Imagine you’re coding a system for a cinema. When a customer buys a ticket, the program prints their details: movie title, showtime, seat, and price. The catch? These details need to appear neatly formatted, not just printed in a messy way.

That’s where Python’s string formatting comes in it lets you insert variables directly into sentences, making the output clean and professional.

Here’s what a ticket looks like:

Ticket python

You’ve been hired to program this ticket generator. To do it properly, you’ll need to learn how to format strings.

The main idea

In Python, you can insert variables into strings using different formatting methods. The most common one today is the f-string (introduced in Python 3.6).

Example:

python
3 lines
|
18/ 500 tokens
1
2
3
movie = "Inception"
seat = "C12"
print(f"Movie: {movie}, Seat: {seat}")
Code Tools

Output:

text
1 lines
|
7/ 500 tokens
1
Movie: Inception, Seat: C12

Instead of printing separately, you combine text and variables in one clean line.

Other formatting options exist, like .format() and %, but f-strings are the simplest and most widely used.

Look at our movie ticket:

text
4 lines
|
15/ 500 tokens
1
2
3
4
=== MOVIE TICKET ===
Movie: Inception
Seat: C12
Price: $12.5

Without formatting, you’d need multiple print() calls. With string formatting, you can combine text and variables in a single structured line.

Quick recap

  • f"..." lets you insert variables directly into strings.
  • Use {} braces around the variable name.
  • Works with numbers, text, and even expressions like {2+3}.

Try it in code

Let’s build a simple ticket with string formatting:

python
10 lines
|
49/ 500 tokens
1
2
3
4
5
6
7
8
9
10
movie = "Inception"
time = "7:30 PM"
seat = "C12"
price = 12.5

print("=== MOVIE TICKET ===")
print(f"Movie: {movie}")
print(f"Showtime: {time}")
print(f"Seat: {seat}")
print(f"Price: ${price}")
Code Tools

Output:

text
5 lines
|
20/ 500 tokens
1
2
3
4
5
=== MOVIE TICKET ===
Movie: Inception
Showtime: 7:30 PM
Seat: C12
Price: $12.5

Learn with AI

If you’re using an AI assistant, try giving this prompt:

Prompt:
Write a Python program that stores details of a movie ticket (title, time, seat, and price) in variables, and prints the ticket using f-strings for formatting.

The AI will generate clean, ready-to-use code with string formatting.

Practice challenge

Fantastic! Now let’s test your knowledge with a new challenge.

Task

Create a ticket for a concert. The details are:

  • Event: Rock Night
  • Venue: City Arena
  • Seat: A45
  • Price: 50.0

The output should be:

text
5 lines
|
21/ 500 tokens
1
2
3
4
5
=== CONCERT TICKET ===
Event: Rock Night
Venue: City Arena
Seat: A45
Price: $50.0

Solution

python
10 lines
|
50/ 500 tokens
1
2
3
4
5
6
7
8
9
10
event = "Rock Night"
venue = "City Arena"
seat = "A45"
price = 50.0

print("=== CONCERT TICKET ===")
print(f"Event: {event}")
print(f"Venue: {venue}")
print(f"Seat: {seat}")
print(f"Price: ${price}")
Code Tools
Perfect! Now you know how to make your program’s output look neat and professional with string formatting.

Frequently Asked Questions

Strings in Python are sequences of characters enclosed in quotes, either single (' ') or double (" ").

A string is a data type used to represent text. Example: name = "John"

Yes, text is considered a string in Python if enclosed in quotes.

Python has one string type, str, which can hold any sequence of characters.

A string variable in Python is a variable that stores a string value, such as name = "Alice".

Still have questions?Contact our support team