Python Forum

Full Version: template string format specifiers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
>>> 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
Its 5.2f not 5:2f
Than you very much!
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.