Python Tutorial- Alignment Function

Python Exercise Lists

Python string provides three methods that can be used to align text, namely

  • ljust() Function
  • rjust() Function
  • center() Function

The following section will introduce their usage one by one.

Python ljust() Function

The function of the ljust() is to fill the specified character to the right of the specified string, so as to achieve the purpose of left-aligning the text.

The basic format of the ljust() function is as follows:

s.ljust (width [, fillchar])

The meaning of each parameter is as follows:

  • s: represents a character string to be filled;
  • width: indicates the total length of the string, including the length of s itself;
  • fillchar: As an optional parameter, it is used to specify the characters used to fill the string. By default, spaces are used.

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.ljust(50))
print(website.ljust(50)

The output is:

www.freelearningpoints.com
www.freelearningpoints.com/python/tutorials/python-numbers

Note that in addition to the URL string that is clearly visible in this output, there are space characters after it, with a total length of 50 characters per line.

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.ljust(50, "-"))
print(website.ljust(50, "-")

The output is:

www.freelearningpoints.com-------------------------
www.freelearningpoints.com/python/tutorials/python-numbers

Python rjust() Function

The rjust () and ljust () functions are similar. The only difference is that the rjust() function is to fill the left side of the string with the specified character, so as to achieve the purpose of right-aligning the text.

The basic format of the rjust() function is as follows:

s.rjust(width[,fillchar])

The meaning of each parameter is exactly the same as ljust(), so it will not be repeated here.

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.rjust(50))
print(website.rjust(50)

The output is:

       www.freelearningpoints.com
www.freelearningpoints.com/python/tutorials/python-numbers

As you can see, each line of string occupies a position of 50 bytes, achieving the overall right-aligned effect.

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.rjust(50))
print(website.rjust(50, "-")

The output is:

       www.freelearningpoints.com
www.freelearningpoints.com/python/tutorials/python-numbers

Python center() Function

The center() string method is similar to the use of ljust() and rjust(), but it centers the text instead of left or right.

The basic format of the center() function is as follows:

s.center(width[,fillchar])

The meaning of each parameter is exactly the same as ljust(), rjust(), so it will not be repeated here.

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.center(50))
print(website.center(50))

The output is:

     www.freelearningpoints.com
www.freelearningpoints.com/python/tutorials/python-numbers

[Example:]

string = "www.freelearningpoints.com"
website = "www.freelearningpoints.com/python/tutorials/python-numbers"
print(string.center(50))
print(website.center(50, "-"))

The output is:

     www.freelearningpoints.com
www.freelearningpoints.com/python/tutorials/python-numbers

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises