Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Substring a String in Python
Next article icon

String Alignment in Python f-string

Last Updated : 22 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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")  

Output
Hello     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} ") 

Output
Name: 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}") 

Output
Name      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") 

Output
Apple    - 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 =.



Next Article
How to Substring a String in Python

S

smarthardik10
Improve
Article Tags :
  • Python
  • python-string
Practice Tags :
  • python

Similar Reads

  • String to Int and Int to String in Python
    Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have
    2 min read
  • 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
  • Python String strip() Method
    strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original. Let's take an example to remove whitespace from both ends of a string. [GFGTABS] Python s =
    2 min read
  • String Slicing in Python
    String slicing in Python is a way to get specific parts of a string by using start, end and step values. It’s especially useful for text manipulation and data parsing. Let’s take a quick example of string slicing: [GFGTABS] Python s = "Hello, Python!" print(s[0:5]) [/GFGTABS]OutputHello Ex
    4 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}"
    3 min read
  • Convert String to Set in Python
    There are multiple ways of converting a String to a Set in python, here are some of the methods. Using set()The easiest way of converting a string to a set is by using the set() function. Example 1 : [GFGTABS] Python s = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s
    1 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. Example[GFGTABS] Python s = "GeeksForGeeks" print(s.rfind("Geeks")) [/GFGTABS]Output8 Explanation string "GeeksForGeeks" contains the substring
    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 tr
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences