Python Forum
template string format specifiers
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
Reply
#2
Its 5.2f not 5:2f
Recommended Tutorials:
Reply
#3
Than you very much!
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Set string in custom format korenron 4 1,047 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  Format String NewPi 2 908 Oct-10-2022, 05:50 PM
Last Post: NewPi
  TypeError: not enough arguments for format string MaartenRo 6 2,858 Jan-09-2022, 06:46 PM
Last Post: ibreeden
  string format challenge jfc 2 1,741 Oct-23-2021, 10:30 AM
Last Post: ibreeden
  Print first day of the week as string in date format MyerzzD 2 1,979 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  string.format() suddenly causing errors with google drive API zwitrader 0 1,733 Jun-28-2021, 11:38 PM
Last Post: zwitrader
  String to Date format SAF 2 2,412 Apr-06-2021, 02:09 PM
Last Post: snippsat
  MySQLdb._exceptions.ProgrammingError: not enough arguments for format string. farah97 0 3,297 Jan-22-2020, 03:49 AM
Last Post: farah97
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 2,987 Jul-01-2019, 03:06 AM
Last Post: metulburr
  write image into string format into text file venkat18 2 4,347 Jun-01-2019, 06:46 AM
Last Post: venkat18

Forum Jump:

User Panel Messages

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