Python Forum
template string format specifiers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: template string format specifiers (/thread-15318.html)



template string format specifiers - VincentLamphier - Jan-13-2019

>>> 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


RE: template string format specifiers - metulburr - Jan-13-2019

Its 5.2f not 5:2f


RE: template string format specifiers - VincentLamphier - Jan-13-2019

Than you very much!


RE: template string format specifiers - snippsat - Jan-13-2019

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.