×

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

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

PyShark in Python

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....

4 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

Python Struct

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.

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.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python Win32 Process

The Win32 process is essentially a Python procedure. This program provides access to advanced Win32 process creation and management features. Process objects are created through the Create method (the constructor)....

6 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Converting Set to List in Python

Converting ‘set’ datatype to ‘list’ datatype is called typecasting. Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called...

3 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.