Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python string alignment methods: ljust(), rjust() and center()

In Python, strings have built-in alignment methods that help you format strings by adding padding characters to adjust their alignment. In this tutorial, we'll cover the ljust(), rjust(), and center() methods.

1. ljust()

The ljust() method returns a left-justified version of the string it is called on. It takes two arguments:

  • width: The total width of the resulting string, including the original string and any padding characters.
  • fillchar (optional): The character used for padding. By default, it is a space character ' '.
text = "Python"
left_aligned = text.ljust(10, '-')
print(left_aligned)  # Output: Python----

2. rjust()

The rjust() method returns a right-justified version of the string it is called on. It takes the same arguments as ljust():

  • width: The total width of the resulting string, including the original string and any padding characters.
  • fillchar (optional): The character used for padding. By default, it is a space character ' '.
text = "Python"
right_aligned = text.rjust(10, '-')
print(right_aligned)  # Output: ----Python

3. center()

The center() method returns a centered version of the string it is called on. It takes the same arguments as ljust() and rjust():

  • width: The total width of the resulting string, including the original string and any padding characters.
  • fillchar (optional): The character used for padding. By default, it is a space character ' '.
text = "Python"
centered = text.center(10, '-')
print(centered)  # Output: --Python--

Example

Here's an example using all three alignment methods to format a table:

headers = ["ID", "Name", "Score"]
data = [
    (1, "Alice", 85),
    (2, "Bob", 90),
    (3, "Carol", 78),
]

# Print headers
print(headers[0].ljust(4) + headers[1].ljust(10) + headers[2].rjust(5))

# Print data
for row in data:
    id, name, score = row
    print(str(id).ljust(4) + name.ljust(10) + str(score).rjust(5))

Output:

ID  Name      Score
1   Alice        85
2   Bob          90
3   Carol        78

In this example, we used ljust() to left-align the "ID" and "Name" columns, and rjust() to right-align the "Score" column.

  1. ljust() Method in Python:

    • The ljust() method left-aligns a string within a specified width by padding it with a specified character (default is space).
    # Example
    text = "Left Align"
    aligned_text = text.ljust(20, '-')
    
  2. rjust() Method in Python:

    • The rjust() method right-aligns a string within a specified width by padding it with a specified character.
    # Example
    text = "Right Align"
    aligned_text = text.rjust(20, '*')
    
  3. center() Method in Python Strings:

    • The center() method centers a string within a specified width by padding it with a specified character.
    # Example
    text = "Center"
    aligned_text = text.center(20, '+')
    
  4. How to Left-Align a String in Python:

    • Use ljust() method for left alignment.
    # Example
    text = "Left Align"
    aligned_text = text.ljust(20)
    
  5. Right-Aligning Strings with rjust() in Python:

    • Use rjust() method for right alignment.
    # Example
    text = "Right Align"
    aligned_text = text.rjust(20)
    
  6. Centering Strings Using center() in Python:

    • Use center() method for center alignment.
    # Example
    text = "Center"
    aligned_text = text.center(20)
    
  7. String Padding in Python with ljust(), rjust(), and center():

    • Padding can be achieved by specifying a width and a padding character.
    # Example
    text = "Padding"
    left_padded = text.ljust(15, '.')
    right_padded = text.rjust(15, '*')
    center_padded = text.center(15, '_')
    
  8. Adjusting String Width in Python:

    • Adjust string width using alignment methods.
    # Example
    text = "Adjust Width"
    adjusted_text = text.ljust(25)  # Adjust width as needed
    
  9. Formatting Text Alignment in Python:

    • Adjust text alignment for better readability.
    # Example
    title = "Formatted Text"
    formatted_title = title.center(30, '=')