Python string provides three methods that can be used to align text, namely
The following section will introduce their usage one by one.
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:
[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
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
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)More Python Exercises:
Python String Exercises