Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Python String - ljust(), rjust(), center()
Next article icon

Python String - ljust(), rjust(), center()

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Strings are sequences of characters that we can manipulate in many ways. Sometimes we need to align a string to the left, right, or centre within a given space. In this article, we will explore the Difference between ljust(), just() and center().

Table of Content

  • str.ljust()
    • Syntax of ljust()
    • Example of ljust()
  • str.rjust()
    • Syntax of rjust()
    • Example of rjust()
  • str.center()
    • Syntax of ljust()
    • Example of center()

str.ljust()

ljust() method is used to align a string to the left side and fill the remaining space on the right with a specific character (or space by default).

Syntax of ljust()

string.ljust(width, fillchar)

Parameters

  • width: The total length of the string after padding.
  • fillchar: The character to pad the string with (optional, default is space).

Return Type:

  • The return type of ljust() is a new string that is left-justified with padding.

Example of ljust()

Python
s = "Burger"   # Padding the name with dashes until length is 20 formatted_item = s.ljust(20, "-")  print(formatted_item)  

Output
Burger-------------- 

str.rjust()

The rjust() method aligns the string to the right side and fills the remaining space on the left with a specific character (or space by default).

Syntax of rjust()

string.rjust(width, fillchar)

Parameters

  • width: The total length of the string after padding.
  • fillchar: The character to pad the string with (optional, default is space).

Return Type:

  • The return type of rjust() is a new string that is right-justified with padding.

Example of rjust()

Python
a = "Hello"  # Adding dashes to the left until length is 10 b = a.rjust(10, "-")   print(b)  

Output
-----Hello 

str.center()

The center() method centers the string in the middle and fills both sides with a specific character (or space by default) to achieve a given width.

Syntax of ljust()

string.center(width, fillchar)

Parameters

  • width: The total length of the string after padding.
  • fillchar: The character to pad the string with (optional, default is space).

Return Type:

  • The return type of center() is a new string that is centered with padding on both sides.

Example of center()

Python
a = "Hello"   # Adding dashes to both sides until length is 10 b = a.center(10, "-")  print(b)   

Output
--Hello--- 

Next Article
Python String - ljust(), rjust(), center()

S

Shambhavi Singh 1
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Python-Built-in-functions
  • python-string
Practice Tags :
  • python

Similar Reads

    String rjust() and ljust() in python()
    1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string. Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the origin
    2 min read
    Python String rjust() Method
    The rjust() method in Python is a string method used to right-align a string within a specified width by padding it with a specified character (default is a space). This method is helpful when you want to align text or numbers for formatting purposes.Pythons = 'geeks' res = s.rjust(10) print(res)Out
    2 min read
    Python String center() Method
    center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want. Example:Pythons1 = "hello" s2 = s1.center(20) print(s2)Output hello Explanation: Here, the
    2 min read
    Python | Pandas Series.str.center()
    Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.center() function is used for filling left and right side of strings in the Series/Index with an additional character. The function is equivalent to Python's str.center(). Syntax:
    3 min read
    Python | Pandas Series.str.ljust() and rjust()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .ljust() and .rjust() are Text methods used to deal with text data in series. S
    3 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