-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Python provides several ways to format strings — combining text with variables or data, while keeping the output readable and clean.
format()
MethodYou can insert variables into strings using {}
placeholders and .format()
.
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
✅ Output: My name is Alice and I am 25 years old.
You can specify the order using numbers or names.
print("Hello {0}, your score is {1}".format("Bob", 90))
print("Name: {n}, Age: {a}".format(n="Sara", a=22))
The cleanest and fastest way to format strings in modern Python.
name = "John"
score = 95
print(f"Hello {name}, your score is {score}")
✅ Output: Hello John, your score is 95
You can format numbers (decimals, padding, etc.) using format specifiers.
pi = 3.14159
print("Value of pi: {:.2f}".format(pi)) # 2 decimal places
num = 5
print(f"{num:03}") # Pads with zeros: 005
You can align strings using :<
, :>
, and :^
.
txt = "Hello"
print(f"{txt:<10}") # Left align
print(f"{txt:>10}") # Right align
print(f"{txt:^10}") # Center align
To print curly braces {}
, use double braces {{
and }}
.
print("This is a {{bracket}}")
Q1. Write a Python program to print "Hello, my name is Alice" using the .format()
method.
Q2. Write a Python program to use keyword arguments with .format()
to insert a name and age into a sentence.
Q3. Write a Python program to display the number 3.4567
rounded as 3.46
using string formatting.
Q4. Write a Python program to print 007
using string formatting to keep leading zeros.
Q5. Write a Python program to use an f-string
to show a total score stored in a variable.
Q6. Write a Python program to print a centered title using the ^
alignment symbol in string formatting.
Q7. Write a Python program to format a table with aligned text and numeric values using formatting specifiers.
Q8. Write a Python program to escape curly braces in a string and display {Hello}
.
Q9. Write a Python program to combine a string and an integer using string formatting.
Q10. Write a Python program to format a price to 2 decimal places and prefix it with a $
symbol.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms