0% found this document useful (0 votes)
2 views4 pages

Python String Format Detailed Guide

This document is a comprehensive guide to Python's string format() method, detailing its usage from basic to advanced concepts. It covers various formatting techniques including multiple values, positional indexing, named placeholders, floating point precision, and more, with examples and expected outputs for clarity. Additionally, it compares format() with f-strings, highlighting their differences and use cases.

Uploaded by

hustlemind918
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python String Format Detailed Guide

This document is a comprehensive guide to Python's string format() method, detailing its usage from basic to advanced concepts. It covers various formatting techniques including multiple values, positional indexing, named placeholders, floating point precision, and more, with examples and expected outputs for clarity. Additionally, it compares format() with f-strings, highlighting their differences and use cases.

Uploaded by

hustlemind918
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python String format() – Complete Detailed

Guide
This document provides a clear, beginner-to-advanced explanation of Python's string format()
method. Each concept includes explanation, input code, and expected output for easy
understanding.
1. Basic Usage of format()
The format() method replaces curly braces {} in a string with values passed to it.
Input:
name = "Ram" print("Hello {}".format(name))
Expected Output:
Hello Ram

2. Multiple Values
Multiple placeholders are replaced in the order values are provided.
Input:
name = "Ram" age = 25 print("My name is {} and age is {}".format(name,
age))
Expected Output:
My name is Ram and age is 25

3. Positional Indexing
Indexes inside {} control which argument is placed where.
Input:
print("{1} {0}".format("World", "Hello"))
Expected Output:
Hello World

4. Named Placeholders
Named placeholders improve readability and reduce errors.
Input:
print("Name: {name}, Age: {age}".format(name="Ram", age=25))
Expected Output:
Name: Ram, Age: 25

5. Floating Point Precision


You can control decimal places using format specifiers.
Input:
print("Value: {:.2f}".format(12.3456))
Expected Output:
Value: 12.35

6. Width and Zero Padding


Width defines minimum space. Zero-padding fills unused space with zeros.
Input:
print("{:5}".format(42)) print("{:05}".format(42))
Expected Output:
42
00042
7. Text Alignment
Strings can be aligned left, right, or center within a given width.
Input:
print("|{:10}|".format("Hi")) print("|{:>10}|".format("Hi"))
print("|{:^10}|".format("Hi"))
Expected Output:
|Hi |
| Hi|
| Hi |
8. Formatting Dates
format() supports date formatting using strftime-style codes.
Input:
from datetime import datetime today = datetime(2026, 1, 19)
print("{:%d-%m-%Y}".format(today))
Expected Output:
19-01-2026

9. Using Lists with format()


List elements can be accessed using index notation.
Input:
marks = [90, 85] print("Math: {0[0]}, Science: {0[1]}".format(marks))
Expected Output:
Math: 90, Science: 85

10. Using Dictionaries with format()


Dictionary keys can be directly referenced inside placeholders.
Input:
data = {"name": "Ram", "age": 25} print("Name: {name}, Age:
{age}".format(**data))
Expected Output:
Name: Ram, Age: 25

11. Escaping Curly Braces


Double braces {{ }} are used to print literal braces.
Input:
print("Set: {{1, 2, 3}}")
Expected Output:
Set: {1, 2, 3}

12. format() vs f-strings


While f-strings are newer and simpler, format() is still widely used in libraries and older Python
versions.
Input:
name = "Ram" print("Hello {}".format(name)) print(f"Hello {name}")
Expected Output:
Hello Ram
Hello Ram

You might also like