Python Forum
confused about string formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
confused about string formatting
#1
I am confused about string formatting.

If I have a number 14.20003 I want to make it into a string s = 14.20

Do I use the "f" format or the "%" format?

what is the best way for the above example.
Reply
#2
a = f"{14.2000003:0.2f}"
b = "{:0.2f}".format(14.2000003)
c = "%0.2f" % 14.2000003
print(a, b, c)
Which do you like best? They all work fine.
Output:
14.20 14.20 14.20
I would go with the top since this is the most recent way to format. It must improve over shortcomings in the previous methods. I like how you embed the value in the same string as the formatting controls. b uses pre-3.6 Python formatting. I don't like how the values are at the end. It is easy to make a mistake on format strings with lots of values. The % formatting is ancient.
Reply
#3
What you say works. Thanks
Reply
#4
f-string is definitely what you should use now,it's more readable and also faster.
If make some more variables it easy to see this they are where the belong in string and not last.
>>> store = 'Walmart'
>>> fruit = 'apple'
>>> cost = 14.20003
>>> print(f'A {fruit} at {store} cost {number:0.2f}')
A apple at Walmart cost 14.20
Vs .format() that was new in Python 2.6
>>> print('A {} at {} cost {:0.2f}'.format(fruit, store, cost))
A apple at Walmart cost 14.20
A couple more.
# f-strings can take any Python expressions inside the curly braces.
>>> cost = 99.75999
>>> finance = 50000
>>> print(f'Toltal cost {cost + finance:.2f}')
Toltal cost 50099.76

>>> name = 'f-string'
>>> print(f"String formatting is called {name.upper():*^20}")
String formatting is called ******F-STRING******

>>> for word in 'f-strings are cool'.split():
...     print(f'{word.upper():~^20}')
...
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~~~COOL~~~~~~~~
Reply
#5
This is good info.

But, if I had a number like 1.12345

How would I format it to read 01.12 ?
Reply
#6
print(f"{1.2345:05.2f}")
print(f"{1.2345:*>5.2f}")
print(f"{1.2345:*<5.2f}")
Output:
01.23 *1.23 1.23*
Reply
#7
So the 05 means a total of 5 spaces (including the decimal) and the.2 means 2 places to the right of the decimal.

Got it.
Reply
#8
If lazy 💤
>>> n = 1.12345
>>> print(f'Number is 0{n:0.2f}')
Number is 01.12 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 247 Jun-08-2024, 05:16 PM
Last Post: tester_V
  String int confused janeik 7 1,288 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,672 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  I am confused with the key and value thing james1019 3 1,096 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  String formatting (strptime) issues Henrio 2 992 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  string formatting barryjo 7 2,266 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 12,754 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Pandas confused DPaul 6 2,777 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,878 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 5,215 Apr-12-2021, 03:03 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020