×

Python String ljust() method

Python String ljust() method

The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default).

Syntax

String.ljust(width[, fillchar])

Parameter

width: This parameter represents the length of the returned string.

fillchar(Optional): This parameter represents a character to fill the missing space (to the right of the string). Default is " " (space).

Return

This method returns the string left a justified string of the given width. The original string is returned if the width is less than the length of the String.

Example 1

 # Python program explaining
 # the string.ljust() method
 # initializing the string
 string = "Python"
 # initializing the length
 width=21
 # string with " " padding 
 val = string.ljust(width) # default padding value is " "
 # printing the left aligned string
 print(val, "is my favorite programming language.") 

Output

Python                is my favorite programming language.

Example 2

 # Python program explaining
 # the string.ljust() method
 # initializing the string
 string = "Hello World"
 # Printing the actual string 
 print ("The actual string:", string, "\n") 
 # Printing the left aligned  
 # string with "!" padding  
 print ("The left aligned string:", string.ljust(40, '!')) 

Output

The actual string: Hello World 
The left aligned string: Hello World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Example 3

 # Python program explaining
 # the string.ljust() method
 # initializing the string
 string = "Morning"
 # Printing the actual string 
 print ("The actual string:", string, "\n") 
 # Printing the left aligned  
 # string with "*" padding  
 print ("The left aligned string with width 40:", string.ljust(40, '*')) 
 # will return the same string the width is less than the length of the string
 print("The left aligned string with width 2:", string.ljust(2, '*')) 

Output

 The actual string: Morning 
 The left aligned string with width 40: Morning*********************************
 The left aligned string with width 2: Morning 

Related Topics

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python vs R

Python: - Python is a popular high-level, general-purpose programming language. Python (the most recent version is Python 3) is a programming language used in the software industry for website development, machine...

3 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.