Fix Python – String formatting in Python 3
I do this in Python 2:
“(%d goals, $%d)” % (self.goals, self.penalties)
What is the Python 3 version of this?
I tried searching for examples online but I kept getting Python 2 versions.
….
I do this in Python 2:
“(%d goals, $%d)” % (self.goals, self.penalties)
What is the Python 3 version of this?
I tried searching for examples online but I kept getting Python 2 versions.
….
I am using the standard json module in python 2.6 to serialize a list of floats. However, I’m getting results like this:
>>> import json
>>> json.dumps([23.67, 23.97, 23.87])
‘[23.670000000000002, 23.969999999999999, 23.870000000000001]’
I want the floats to be formated with only two decimal digits. The output should look like this:
>>> json.dump….
Is there a format for printing Python datetimes that won’t use zero-padding on dates and times?
Format I’m using now:
mydatetime.strftime(‘%m/%d/%Y %I:%M%p’)
Result: 02/29/2012 05:03PM
Desired: 2/29/2012 5:03PM
What format would represent the month as ‘2’ instead of ’02’, and time as ‘5:03PM’ instead of ’05:03PM’
….
I construct a string s in Python 2.6.5 which will have a varying number of %s tokens, which match the number of entries in list x. I need to write out a formatted string. The following doesn’t work, but indicates what I’m trying to do. In this example, there are three %s tokens and the list has three entries.
s = ‘%s BLAH %s FOO %s BAR’
x = [‘1’, ….
I need to write the below data to yaml file using Python:
{A:a, B:{C:c, D:d, E:e}}
i.e., dictionary in a dictionary. How can I achieve this?
….
I am looking to format a number like 188518982.18 to £188,518,982.18 using Python.
How can I do this?
….
I’m trying to convert an integer to binary using the bin() function in Python. However, it always removes the leading zeros, which I actually need, such that the result is always 8-bit:
Example:
bin(1) -> 0b1
# What I would like:
bin(1) -> 0b00000001
Is there a way of doing this?
….
This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 16 days ago.
How would you go about formatting a long line such as this? I’d like to get it to no more than 80 ….
How can I format a float so that it doesn’t contain trailing zeros? In other words, I want the resulting string to be as short as possible.
For example:
3 -> “3”
3. -> “3”
3.0 -> “3”
3.1 -> “3.1”
3.14 -> “3.14”
3.140 -> “3.14”
….
This question already has answers here:
How to print without a newline or space
(25 answers)
Closed 5 years ago.
In python, if I say
print ‘h’
I get the letter h and a newline. If I say
print ‘h’,
I get the letter h and ….