string-formatting

Python formatting integer (fixed digits before/after the decimal point)

Python formatting integer (fixed digits before/after the decimal point) Question: I was wondering if it’s possible to use two format options together when formatting integers. I know I can use the bellow to include zero places varInt = 12 print( “Integer : ” + “{:03d}”.format(varInt) ) To get the output “Integer : 012” I can …

Total answers: 4

Use str.format() to access object attributes

Use str.format() to access object attributes Question: I have a Python object with attributes a, b, c. I still use old string formatting, so I’d normally print these manually: print ‘My object has strings a=%s, b=%s, c=%s’ % (obj.a, obj.b, obj.c) Lately, my strings have been getting super long, and I’d much rather be able …

Total answers: 6

Python TypeError: non-empty format string passed to object.__format__

Python TypeError: non-empty format string passed to object.__format__ Question: I hit this TypeError exception recently, which I found very difficult to debug. I eventually reduced it to this small test case: >>> “{:20}”.format(b”hi”) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: non-empty format string passed to object.__format__ This is very non-obvious, …

Total answers: 2

How to truncate a string using str.format in Python?

How to truncate a string using str.format in Python? Question: How to truncate a string using str.format in Python? Is it even possible? There is a width parameter mentioned in the Format Specification Mini-Language: format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] … width ::= integer … But specifying it apparently only works for padding, not truncating: >>> ‘{:5}’.format(‘aaa’) ‘aaa …

Total answers: 2

Table equating old and new Python string formatting

Table equating old and new Python string formatting Question: Is there a good reference which directly compares examples of old % style string formatting with the equivalent in the newer .format way? I’m looking for something along the lines of this table (which in that case compares matlab commands with the equivalent in numpy). This …

Total answers: 1

Plural String Formatting

Plural String Formatting Question: Given a dictionary of ints, I’m trying to format a string with each number, and a pluralization of the item. Sample input dict: data = {‘tree’: 1, ‘bush’: 2, ‘flower’: 3, ‘cactus’: 0} Sample output str: ‘My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti’ It needs to …

Total answers: 7

Converting Float to Dollars and Cents

Converting Float to Dollars and Cents Question: First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am …

Total answers: 6

How to get Python to gracefully format None and non-existing fields

How to get Python to gracefully format None and non-existing fields Question: If I write in Python: data = {‘n’: 3, ‘k’: 3.141594, ‘p’: {‘a’: 7, ‘b’: 8}} print(‘{n}, {k:.2f}, {p[a]}, {p[b]}’.format(**data)) del data[‘k’] data[‘p’][‘b’] = None print(‘{n}, {k:.2f}, {p[a]}, {p[b]}’.format(**data)) I get: 3, 3.14, 7, 8 Traceback (most recent call last): File “./funky.py”, line …

Total answers: 3

format strings and named arguments in Python

format strings and named arguments in Python Question: Case 1: "{arg1} {arg2}".format(10, 20) It will give KeyError: ‘arg1′ because I didn’t pass the named arguments. Case 2: "{arg1} {arg2}".format(arg1=10, arg2=20) Now it will work properly because I passed the named arguments. And it prints ’10 20′ Case 3: And, If I pass wrong name it …

Total answers: 2

Python: Print to one line with time delay between prints

Python: Print to one line with time delay between prints Question: I want to make (for fun) python print out ‘LOADING…’ to console. The twist is that I want to print it out letter by letter with sleep time between them of 0.1 seconds (ish). So far I did this: from time import sleep print(‘L’) …

Total answers: 7