Padding Strings in Python(python字符串填充补齐)

来自 Padding Strings in Python

Introduction

String padding refers to adding, usually, non-informative characters to a string to one or both ends of it. This is most often done for output formatting and alignment purposes, but it can have useful practical applications.

A frequent use case for padding strings is outputting table-like information in a table-like fashion. You can do this in a variety of ways, including using Pandas to convert your data to an actual table. This way, Python would handle the output formatting on its own.

In this article, we'll cover how to pad strings in Python.

Say, we've got these three lists:

medicine1 = ['Morning', 'dispirine', '1 mg']
medicine2 = ['Noon', 'arinic', '2 mg']
medicine3 = ['Evening', 'Long_capsule_name', '32 mg']

We can form these into a string, using the join() function:

print(str.join(' ', medicine1))
print(str.join(' ', medicine2))
print(str.join(' ', medicine3))

Would give us the rather untidy output of:

Morning Dispirine 1 mg
Noon Arinic 2 mg
Evening Long_capsule_name 32 mg

To combat this, we could write for/while loops and append spaces to the strings until they reached a certain length, and make sure that all the data is aligned properly for easy visual inspection. Or, we could use built-in functions that can achieve the same goal.

The functions we'll be taking a look at in this article are: ljust(), center(), rjust(), zfill() and format(). Any of these functions can be used to add a certain number of characters to either end of strings, including spaces.

Padding Types

Before we take a closer look at the functions mentioned above, we'll take a look at different types of padding so we can reference them when talking about the functions.

Left Padding

Adding left padding to a string means adding a given character at the start of a string to make it of the specified length. Left padding, outside of simple formatting and alignment reasons can be really useful when naming files that start with a number generated in a sequence.

For example, you need to name 11 files, and each of them starts with a number from 1 to 11. If you simply added the number at the beginning of the file, most operating systems would sort the files in the following order: 1, 10, 11, 2, and so on.

This happens of course because of the rules of lexicographical sorting, but you can avoid these situations by naming files with one or more leading zeroes, depending on how many files you expect, i.e.: 01, 02, 03...

This can be achieved by effectively left padding the numbers with the appropriate number of zeroes, which keeps their original value.

This gives the effect of strings being left-justified.

Center Padding

This means that the given character is added in equal measure to both sides of the string until the new string reaches the given length. Using this effectively centers the string in the provided length:

This is a normal string.

This is a center padded string.

Right Padding

Right padding is analogous to left padding - the given character is added to the end of the string until the string reaches a certain length.

Python Functions For String Padding

Python offers many functions to format and handle strings, their use depends on the use case and the developer's personal preference. Most of the functions we'll discuss deal with text justification which is essentially adding padding to one side of the string. For example, for a string to be left-justified, we need to add padding to the end (right side) of the string.

Note: In all the functions that expect a width or len parameter, in case the original string is longer than the specified width or len the entire string will be kept without changes. This can have the undesired effect of long strings ruining the formatting, so when choosing a width value, make sure you take your longest string into an account or a top length boundary.

ljust()

The ljust() function aligns a string to the left by adding right padding.

The ljust() function takes two parameters: width and fillchar. The width is mandatory and specifies the length of the string after adding padding, while the second parameter is optional and represents the character added pad the original string.

The default value is a space character, i.e. ' '. This is a particularly good option to use when printing table-like data, like in our example at the beginning:

medicine1 = ['Morning', 'Dispirine', '1 mg']
medicine2 = ['Noon', 'Arinic', '2 mg']
medicine3 = ['Evening', 'Long_capsule_name', '32 mg']

for medicine in [medicine1, medicine2, medicine3]:
    for entry in medicine:
        print(entry.ljust(25), end='')
    print()

Which gives us the output:

Morning                  Dispirine                1 mg                     
Noon                     Arinic                   2 mg                     
Evening                  Long_capsule_name        32 mg 

center()

The center() function aligns a string in the center of the specified width, by adding padding equally to both sides. The parameters are the same as with the ljust() function, a required width, and optional fillchar parameter:

list_of_strings = ["This can give us", "text that's center aligned", "within the specified width"]

for s in list_of_strings:
    print(s.center(50, ' '))

Output:

                 This can give us                 
            text that's center aligned            
            within the specified width     

rjust()

Analogous to the previous two functions, rjust() aligns the string to the right by adding padding to the left (beginning) of the string.

Again, the parameters are the required width and optional fillchar. Like we mentioned previously, this function is very useful when naming files that start with numbers because of the more intuitive sorting:

list_of_names_original = []
list_of_names_padded = []

for n in range(1, 13):
    list_of_names_original.append(str(n) + "_name")
    list_of_names_padded.append(str(n).rjust(2, '0') + "_name")

print("Lexicographical sorting without padding:")
print(sorted(list_of_names_original))
print()

print("Lexicographical sorting with padding:")
print(sorted(list_of_names_padded))

Running this code would give us:

Lexicographical sorting without padding:
['10_name', '11_name', '12_name', '1_name', '2_name', '3_name', '4_name', '5_name', '6_name', '7_name', '8_name', '9_name']

Lexicographical sorting with padding:
['01_name', '02_name', '03_name', '04_name', '05_name', '06_name', '07_name', '08_name', '09_name', '10_name', '11_name', '12_name']

zfill()

The zfill() function performs very similarly to using rjust() with zero as the specified character. It left pads the given string with zeroes until the string reaches the specified length.

The only difference is that in case our string starts with a plus(+) or minus(-) sign, the padding will start after that sign:

neutral = '15'
positive = '+15'
negative = '-15'
length = 4

print(neutral.zfill(length))
print(positive.zfill(length+1))
print(negative.zfill(length+1))

This is done to keep the original value of the number in case the string was a number. Running this code would give us:

0015
+0015
-0015

format()

The format() function is the most advanced in the list. This single function can be used for left, right, and even center padding. It is also used for other formatting too, but we'll only take a look at the padding functionality it provides.

It returns the string after formatting the specified values and putting them inside the string placeholders which are defined by {}.

The placeholders can be identified by either named indexes, numbered indexes, or even empty curly brackets. A quick example of how these placeholders look before we see how we can use this function to add padding:

print("Placeholders can given by {0}, or with {value}".format("adding a number", value="a named value"))
print("They can also be given {}, without adding a {} or {}".format("implicitly", "number", "name"))

Which would give us:

Placeholders can given by adding a number, or with a named value
They can also be given implicitly, without adding a number or name

These placeholders accept a variety of formatting options. Let's see how we can achieve different types of string padding by using these options:

  • Left Padding: Use > inside the placeholder and a number to specify the desired width, to right align a string (append characters at the start):
txt = "We {:>8} Python."
print(txt.format('love'))

Which gives us:

We     love Python.
  • Center Padding: Similarly, we can use ^ for center padding/alignment:
txt = "We {:^8} Python."
print(txt.format('love')) 
We   love   Python.
  • Right Padding: Use < inside placeholders to left align a string:
txt = "We {:<8} Python."
print(txt.format('love')) 
We love     Python.

You can also append characters other than white spaces, by adding the specified characters before the >, ^ or < character:

print('{:*^50}'.format('Center padding with a specific character'))
*****Center padding with a specific character*****

You can read more about the different possibilities of the format() function in our Guide to Formatting Strings with Python.

Conclusion

Adding padding to strings in Python is a relatively straight-forward process, that can noticeably increase the readability of your output, especially if the data you have can be read in a table-like fashion.

In this article, we've covered the ljust(), rjust(), center(), zfill() and format() function as built-in approaches to string padding in Python.

posted on 2022-03-04 10:06  王延刚  阅读(503)  评论(0编辑  收藏  举报