Python String ljust Method - Python Tutorial | Logic Practice
Python Course / Other / Python String ljust Method

Python String ljust Method

BLUF: This lesson on Python String ljust Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String ljust Method

Mastering Python String ljust Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The ljust function in Python is utilized to left-align a string while filling the remaining gaps with specified fill characters. This function generates a new string that is left-justified and supplemented with the designated fill characters.

Syntax of Python String Ijust Method

It has the following syntax:

Example

ljust(width[, fillchar])

Parameters

  • width: width of the given string.
  • fillchar: characters to fill the remaining space in the string. It is optional .
  • Return

It returns a left justified string.

Different Examples for Python String Ijust Method

Let us explore various examples of the ljust method to gain a clearer understanding of its capabilities.

Example 1

Let's consider an example to illustrate the functionality of the ljust method in Python.

Example

# Python ljust() method example

# Variable declaration

str = 'Example'

# Calling function

str = str.ljust(20)

# Displaying result

print(str)

Output:

J a v a t p o i n t

Example 2

To illustrate the functionality of the ljust method in Python, we will use a specific example.

Example

# Python ljust() method example

# Variable declaration

str = 'Example'

# Calling function

str = str.ljust(15,"$")

# Displaying result

print(str)

Output:

J a v a t p o i n t $ $ $ $ $

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience