Python string left alignment
outline
Python String Left Alignment Tutorial
The left alignment of the Python string, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the right side of the string, so as to achieve the purpose of left alignment .
In Python, strings are left justified using the function ljust().
Detailed explanation of Python ljust() function
grammar
S.ljust(width[, fillchar]) -> str
parameter
parameter | describe |
---|---|
s | Indicates the string to pad. |
width | Indicates the total length of the string including the length of S itself. |
fills | As an optional parameter, it is used to specify the characters used to fill the string, and spaces are used by default. |
end | Indicates the end position to end the search. If not specified, it defaults to searching until the end. |
return value
str: returns the filled string.
the case
Pad with blanks
Use the ljust() function to left-justify a string
print("嗨客网(www.haicoder.net)")
# 使用 ljust() 函数,左对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.ljust(100)
print("Len Str =", len(strHaicoder), "Len LStr =", len(lStr))
After the program runs, the console output is as follows:
First, we define a variable strHaicoder of string type, and then we use the ljust() function of the string to make its length 100, and fill it on the right, using left alignment.
Finally, we use the len() function to get the length of the string, and find that the original length is 28, and the filled length is 100.
Fill with specified characters
Use the ljust() function to left-justify a string
print("嗨客网(www.haicoder.net)")
# 使用 ljust() 函数,左对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.ljust(50, '-')
print("Str =", strHaicoder)
print("LStr =", lStr)
After the program runs, the console output is as follows:
First, we define a string type variable strHaicoder, then we use the ljust() function of the string to make its length 50, and fill it on the right, the padding character is , and use left alignment -
.
Finally, we use the print() function to print the unfilled and filled strings.
Summary of Python string left alignment
Left alignment of Python strings, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the right side of the string, so as to achieve the purpose of left alignment .
In Python, strings are left justified using the function ljust(). Python ljust() function syntax:
S.ljust(width[, fillchar]) -> str
Python string center alignment
Python string center alignment tutorial
The center alignment of the Python string, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the left and right sides of the string together, so as to achieve centering alignment purpose.
In Python, strings are centered using the function center().
Detailed explanation of Python center() function
grammar
S.center(width[, fillchar]) -> str
parameter
parameter | describe |
---|---|
s | Indicates the string to pad. |
width | Indicates the total length of the string including the length of S itself. |
fills | As an optional parameter, it is used to specify the characters used to fill the string, and spaces are used by default. |
end | Indicates the end position to end the search. If not specified, it defaults to searching until the end. |
return value
Returns the padded string.
the case
Pad with blanks
Use the center() function to center a string
print("嗨客网(www.haicoder.net)")
# 使用 center() 函数,居中对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.center(100)
print("Len Str =", len(strHaicoder), "Len LStr =", len(lStr))
After the program runs, the console output is as follows:
First, we define a string type variable strHaicoder, then we use the center() function of the string to make the length 100, and fill it on the left and right side at the same time, and use center alignment.
Finally, we use the len() function to get the length of the string, and find that the original length is 28, and the filled length is 100.
Fill with specified characters
Use the center() function to center a string
print("嗨客网(www.haicoder.net)")
# 使用 center() 函数,居中对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.center(50, '-')
print("Str =", strHaicoder)
print("LStr =", lStr)
After the program runs, the console output is as follows:
First, we define a variable strHaicoder of string type, then we use the center() function of the string to make its length 50, and fill it on the left and right side at the same time, the padding character is , and use center alignment -
.
Finally, we use the print() function to print the unfilled and filled strings.
Python string center alignment summary
Center alignment of Python strings, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the left and right sides of the string at the same time, so as to achieve centering alignment purpose.
In Python, strings are centered using the function center(). Python center() function syntax:
S.center(width[, fillchar]) -> str
Python string right alignment
Python string right alignment tutorial
The right alignment of the Python string, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the left side of the string, so as to achieve the purpose of right alignment .
In Python, strings are right justified using the function rjust().
Detailed explanation of Python rjust() function
grammar
S.rjust(width[, fillchar]) -> str
parameter
parameter | describe |
---|---|
s | Indicates the string to pad. |
width | Indicates the total length of the string including the length of S itself. |
fills | As an optional parameter, it is used to specify the characters used to fill the string, and spaces are used by default. |
end | Indicates the end position to end the search. If not specified, it defaults to searching until the end. |
return value
Returns the padded string.
the case
Pad with blanks
Use the rjust() function to right-justify a string
print("嗨客网(www.haicoder.net)")
# 使用 rjust() 函数,右对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.rjust(100)
print("Len Str =", len(strHaicoder), "Len LStr =", len(lStr))
After the program runs, the console output is as follows:
First, we define a variable strHaicoder of string type, and then we use the rjust() function of the string to make its length 100, and fill it on the left, and use right alignment.
Finally, we use the len() function to get the length of the string, and find that the original length is 28, and the filled length is 100.
Fill with specified characters
Use the rjust() function to right-justify a string
print("嗨客网(www.haicoder.net)")
# 使用 rjust() 函数,右对齐字符串
strHaicoder = "I study Python From HaiCoder"
lStr = strHaicoder.rjust(50, '-')
print("Str =", strHaicoder)
print("LStr =", lStr)
After the program runs, the console output is as follows:
First, we define a variable strHaicoder of string type, and then we use the rjust() function of the string to make its length 50, and fill it on the left, the padding character is , and use right alignment -
.
Finally, we use the print() function to print the unfilled and filled strings.
Python string right alignment summary
The right alignment of the Python string, that is, when we need to set the string to a fixed length, if the length of the string is not enough, we can specify the use of specific characters to pad the left side of the string, so as to achieve the purpose of right alignment .
In Python, strings are right justified using the function rjust(). Python rjust() function syntax:
S.rjust(width[, fillchar]) -> str