×

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

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Get Bounding Box Co-ordinates 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....

6 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.