×

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 System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

To Do GUI Application 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...

3 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

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

4 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.