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)
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.
ljust()
Method in Python:
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, '-')
rjust()
Method in Python:
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, '*')
center()
Method in Python Strings:
center()
method centers a string within a specified width by padding it with a specified character.# Example text = "Center" aligned_text = text.center(20, '+')
How to Left-Align a String in Python:
ljust()
method for left alignment.# Example text = "Left Align" aligned_text = text.ljust(20)
Right-Aligning Strings with rjust()
in Python:
rjust()
method for right alignment.# Example text = "Right Align" aligned_text = text.rjust(20)
Centering Strings Using center()
in Python:
center()
method for center alignment.# Example text = "Center" aligned_text = text.center(20)
String Padding in Python with ljust()
, rjust()
, and center()
:
# Example text = "Padding" left_padded = text.ljust(15, '.') right_padded = text.rjust(15, '*') center_padded = text.center(15, '_')
Adjusting String Width in Python:
# Example text = "Adjust Width" adjusted_text = text.ljust(25) # Adjust width as needed
Formatting Text Alignment in Python:
# Example title = "Formatted Text" formatted_title = title.center(30, '=')