Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically calculated zero padding in format string in python

Tags:

So I know that "%02d to %02d"%(i, j) in Python will zero pad i and j.

But, I was wondering if there is any way to dynamically zero pad in a format string (I have a list of numbers and I want the zero pad size to be equal to the longest number in that list).

I know that I could use zfill, but I was hoping I could do the entire thing in a format string.

like image 966
Daniel Gorelik Avatar asked Aug 01 '13 21:08

Daniel Gorelik


People also ask

How do you zero a padding in Python?

Python String zfill() MethodThe zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length. If the value of the len parameter is less than the length of the string, no filling is done.

What does {: 3f mean in Python?

"f" stands for floating point. The integer (here 3) represents the number of decimals after the point. "%. 3f" will print a real number with 3 figures after the point. – Kefeng91.

How do you padding a string in Python?

The standard way to add padding to a string in Python is using the str. rjust() function. It takes the width and padding to be used. If no padding is specified, the default padding of ASCII space is used.

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42)) Would output Joe is 42 years old.

What is the use of padding in Python?

Inserting non-informative characters to a string is known as Padding. The insertion can be done anywhere on the string. String padding is useful for output formatting and alignment of strings while printing. Even for displaying values like a table string Padding is required.

How to pad the integer number in Python?

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.

How to pad strings in Python?

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.

How do I format an F-string in Python?

The string itself can be formatted in much the same way that you would with str.format (). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting. print(f"My name is {name}.")


2 Answers

Use the str.format() method of string formatting instead:

'{number:0{width}d}'.format(width=2, number=4)

Demo:

>>> '{number:0{width}d}'.format(width=2, number=4)
'04'
>>> '{number:0{width}d}'.format(width=8, number=4)
'00000004'

The str.format() formatting specification allows for multiple passes, where replacement fields can fill in parameters for formatting specifications.

In the above example, the hard-coded padding width specification would look like:

'{:02d}'.format(4)

or

'{number:02d}'.format(number=4)

with a named parameter. I've simply replaced the 2 width specifier with another replacement field.

To get the same effect with old-style % string formatting you'd need to use the * width character:

'%0*d' % (width, number)

but this can only be used with a tuple of values (dictionary formatting is not supported) and only applies to the field width; other parameters in the formatting do not support dynamic parameters.

like image 164
Martijn Pieters Avatar answered Oct 14 '22 20:10

Martijn Pieters


Yes, it's possible

>>> print "%0*d" % (5, 4)
00004
>>> print "%0*d" % (10, 4)
0000000004
like image 38
Roman Pekar Avatar answered Oct 14 '22 21:10

Roman Pekar