Python padding что такое

Padding Strings in Python

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.

Читайте также:  Генерация хэш кода java

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:

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, ' ')) 

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

 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.

Читайте также:  Css увеличить шрифт при наведении

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:

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 , or with ".format("adding a number", value="a named value")) print("They can also be given <>, without adding a <> or <>".format("implicitly", "number", "name")) 
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')) 
txt = "We Python." print(txt.format('love')) 
txt = "We Python." print(txt.format('love')) 

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

print(''.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.

Читайте также:  File caching in java

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

Источник

Python Padding | How to Pad Zeros to Number or String?

Many times to represent the data or display it on the output console, we need to pad a number with extra zeros to make it looks consistent.

In this post, I a going to share the simplest two methods for Python padding.

Before jumping into the code, let’s see…

If you have the serial number from 0 to 999. It looks something like this…

0 1 2 . . 8 9 . . 25 26 17 . . 997 998 999

The number series does not look fancy as the length of the number in the series varies. If we pad the number to make it have the same length (says 3), it looks like this…

If we pad the number to make it have the same length (says 3), it looks like this…

000 001 002 . . 008 009 . . 025 026 017 . . 997 998 999

From the above two number series list, which one does look good?

So let’s see how to pad integer number and string in Python.

Python Padding:

There are multiple methods of doing this. Let’s check some of the simple Python tricks…

Method 1: Using zfill()

strNum = '7' print strNum.zfill(3)

The zfill is a function associated with the string object. 3 is the expected length of the string after padding.

Method 2: Using rjust()

The rjust() is string inbuilt function in Python. Pass the expected length of the string after padding and character to be used for padding as two parameters of the function.

With this function, you can use any character for padding.

Output:

Both the method will give the same output as…

“1” gives out as a string of “001”

“2” gives “002”, “99” gives “099”, “100” gives “100”, “999” gives “999”

Exception: If the length of the given string is greater than the padding length, it returns the same string.

Above code returns “7000” for “7000” even if you pass 3 as expected length after padding.

In Python, you can not pad the integer number. You have to convert the number into the string. And then pad it.

Conversion of the integer into the string is very easy.

Now you can make the consistent length for all the numbers in series.

This function will be more helpful if you have a function that manipulates the same length of the input string.

This is all about Python padding. There are so many other methods of doing this. To make it simple to use in your code, I have listed the simplest two methods.

If you think as there are other simplest ways of solving this problem, comment below. I am sure it will be helpful other Python Programmers.

Источник

Оцените статью