Bottom Page

Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 template string format specifiers
#1
>>> f'{id:3s} : {location:19s} : {max_temp:3d} / {min_temp:3d} / {precipitation:5:2f}'.format(
id=id, location=location, max_temp=max_temp,
min_temp=min_temp, precipitation=precipitation
)
Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
f'{id:3s} : {location:19s} : {max_temp:3d} / {min_temp:3d} / {precipitation:5:2f}'.format(
ValueError: Invalid format specifier

--Why am I getting this error? Please help. Thanks. All variables are assigned and match
Quote
#2
Its 5.2f not 5:2f
Quote
#3
Than you very much!
metulburr likes this post
Quote
#4
You are also mixing f-string and format().
It can be done a easier with f-string,then there no need for fomat() at all.
id = 1000
location = 'fooooooooo'
max_temp = 40000.458545
min_temp = 1000.7895642
precipitation = 7575.45454

print(f'{id:d} : {location:.3} : {max_temp:.2f} / {min_temp:.2f} / {precipitation:.1f}')
Output:
1000 : foo : 40000.46 / 1000.79 / 7575.5
Also id is a name used bye Python,so use something else eg temp_id = 1000.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  changing format to Int Scott 1 27 1 hour ago
Last Post: ODIS
  Date format issue the_dude 11 504 Jun-07-2019, 06:27 AM
Last Post: snippsat
  write image into string format into text file venkat18 2 188 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Changing Number Format moby 4 130 May-24-2019, 11:04 PM
Last Post: snippsat
  changing { and } in str.format() Skaperen 10 344 May-16-2019, 05:07 AM
Last Post: Skaperen
  Return in correct json format UtiliseIT 3 242 May-13-2019, 11:24 AM
Last Post: snippsat
  error: self.name = "NPC{}".format(num_of_NPCs + 1) Exsul 1 79 May-03-2019, 08:56 PM
Last Post: ichabod801
  Format for elif sunilramakumar 4 232 Apr-08-2019, 05:21 PM
Last Post: Yoriz
  Convert SAS Dates Format in a loop MohanReddy 2 184 Apr-02-2019, 10:31 AM
Last Post: scidam
  Help regarding date format Prince_Bhatia 2 157 Apr-01-2019, 05:46 AM
Last Post: Prince_Bhatia

Forum Jump:


Users browsing this thread: 1 Guest(s)