String Alignment in Python f-string
Last Updated :
22 Mar, 2025
String alignment in Python helps make text look neat and organized, especially when printing data of different lengths. Without formatting, output can appear messy and hard to read. Python’s f-strings make it easy to align text by controlling its position within a set space. Python’s f-strings allow text alignment using three primary alignment specifiers:
- < (Left Alignment): Aligns the text to the left within the specified width.
- > (Right Alignment): Aligns the text to the right within the specified width.
- ^ (Center Alignment): Centers the text within the specified width.
Each alignment specifier is followed by a width number, which determines how much space is reserved for the string.
Example: Left-aligned (<), Right-aligned (>) and Center-aligned (^)
Python
s = "Hello"
print(f"{s:<10}World")
print(f"{s:>10}World")
print(f"{s:^10}World")
OutputHello World
HelloWorld
Hello World
Explanation: < specifier left-aligns "Hello", so spaces are added to the right. The > specifier right-aligns "Hello", meaning spaces are added before it. The ^ specifier centers "Hello", adding an equal number of spaces on both sides.
Examples of String Alignment in Python f-strings
Example 1: Aligning Multiple Variables in a Table-Like Format
Python
a = "Alice"
b = 25
# Left-aligned (<), Right-aligned (>), and Center-aligned (^)
print(f"Name: {a:<10} Age: {b:>5} ")
print(f"Name: {a:^10} Age: {b:^5} ")
OutputName: Alice Age: 25
Name: Alice Age: 25
Explanation: "Alice" is left-aligned in 10 spaces and age 25 is right-aligned in 5 spaces for consistency. The second print statement centers both within their widths.
Example 2: Displaying List Data in a Structured Table
Python
d = [("Alice", 25, "USA"), ("Char", 22, "Chicago")]
print(f"{'Name':<10}{'Age':<5}{'City':<10}")
for name, age, city in d:
print(f"{name:<10}{age:<5}{city:<10}")
OutputName Age City
Alice 25 USA
Char 22 Chicago
Explanation: Column headers are left-aligned for a neat presentation. The loop aligns values within fixed-width columns, ensuring organized output with consistent spacing.
Example 3: Handling dynamic width
Python
a = ["Apple", "Banana", "Cherry"]
max_width = max(len(word) for word in a) + 2
for word in a:
print(f"{word:<{max_width}} - Fruit")
OutputApple - Fruit
Banana - Fruit
Cherry - Fruit
Explanation: max_width dynamically calculates the longest word's length plus 2 spaces for padding, ensuring left-aligned, consistently spaced and readable output.
Example 4: String padding for enhanced formatting
Python
s = "Python"
print(f"{s:-^20}")
print(f"{s:*<15}")
print(f"{s:=>15}")
Output-------Python-------
Python*********
=========Python
Explanation: The first print statement centers "Python" within 20 spaces, filling the empty space with -. The second line left-aligns "Python" within 15 spaces, filling the remaining space with *. The third line right-aligns "Python" within 15 spaces, filling the left side with =.
Similar Reads
How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str
4 min read
Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
8 min read
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
Convert Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
2 min read
Python String rfind() Method Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.ExamplePythons = "GeeksForGeeks" print(s.rfind("Geeks"))Output8 Explanationstring "GeeksForGeeks" contains the substring "Geeks" twice.rfind() method starts the sea
4 min read
Working with Strings in Python 3 In Python, sequences of characters are referred to as Strings. It used in Python to record text information, such as names. Python strings are "immutable" which means they cannot be changed after they are created.Creating a StringStrings can be created using single quotes, double quotes, or even tri
5 min read