Python Forum
Invalid syntax with an f-string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid syntax with an f-string
#1
I'm getting invalid syntax with Python showing a carrot under the first colon. What did I do wrong?

                    print({}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {:.2f}, ",", {:.2f}, ",", {:.2f}, ",", {}, ",",
                        {:.2f}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {:.2f}, ",", {:.2f}, ",", {}, ",", {:.2f},
                        ",", {:.2f}, ",", {:.2f}, ",", {:.2f}.format(spread_count, date, trade_date, L_spx, L_strike, L_price,
                        L_iv, L_delta, L_theta, S_price, S_iv, L_exp_mo, L_dte_trade_inception, S_exp_mo, S_dte_trade_inception,
                        spread_width, S_delta, S_theta, P_price, P_delta, P_theta, P_t_d, P_skew), file=strike_file) #testing f-string formatting
Reply
#2
That is not f-string formatting, that is the format() command and it is really, really wrong.

f-string looks like this:
count = 1
price = 1.2
name = "My name"
print(f"{count}, {name}, {price:.2f}")
Reply
#3
(Jan-13-2022, 11:10 PM)deanhystad Wrote: That is not f-string formatting, that is the format() command and it is really, really wrong.

f-string looks like this:
count = 1
price = 1.2
name = "My name"
print(f"{count}, {name}, {price:.2f}")

My mistake... I had that confused.

In any case, what's wrong with the syntax in my usage of the format command here? Note: the output is to a .csv file with over 20 columns, which is why I have all the ",", and such.
Reply
#4
(Jan-13-2022, 11:41 PM)Mark17 Wrote: In any case, what's wrong with the syntax in my usage of the format command here?
Almost all of it is wrong,learn to test smaller part before writing so much.

To give some example how it can be done
import csv

with open('birthday.csv') as csv_file:
    reader = csv.reader(csv_file, delimiter=',')
    for row in reader:
        print('{:<15}  {:<15}  {:<15}'.format(*row))
Output:
name department birthday month John Smith Accounting November Erica Meyers IT March
Pandas and tabulate
from tabulate import tabulate
import pandas as pd

df = pd.read_csv('birthday.csv')
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False))
Output:
+--------------+--------------+------------------+ | name | department | birthday month | |--------------+--------------+------------------| | John Smith | Accounting | November | | Erica Meyers | IT | March | +--------------+--------------+------------------+
CSV file used:
Output:
name,department,birthday month John Smith,Accounting,November Erica Meyers,IT,March
Reply
#5
It looks like this is what you're looking for:
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print ('{0:.2f},{1},{2},{3}'.format(spread_count, date, trade_date, L_spx, L_strike))
Output:
27.25,Jan 13 2022,yesterday,144
Mark17 likes this post
Reply
#6
(Jan-14-2022, 02:15 AM)BashBedlam Wrote: It looks like this is what you're looking for:
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print ('{0:.2f},{1},{2},{3}'.format(spread_count, date, trade_date, L_spx, L_strike))
Output:
27.25,Jan 13 2022,yesterday,144

Nice... looks like it works without the numbers too and I don't need all those extra commas in quotes to get the values into separate cells. Thanks!
Reply
#7
With f-string that preferable and modern way to use now,it would be like.
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print (f'{spread_count:.2f},{date},{trade_date},{L_strike}')
Output:
27.25,Jan 13 2022,yesterday,None
Python 3's f-Strings: An Improved String Formatting Syntax (Guide)
Mark17 likes this post
Reply
#8
(Jan-14-2022, 03:37 PM)snippsat Wrote: With f-string that preferable and modern way to use now,it would be like.
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print (f'{spread_count:.2f},{date},{trade_date},{L_strike}')
Output:
27.25,Jan 13 2022,yesterday,None
Python 3's f-Strings: An Improved String Formatting Syntax (Guide)

I'll try to start using f-strings and getting used to this. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,202 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  syntax error question - string mgallotti 5 1,318 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  SyntaxError: invalid syntax ?? korenron 15 5,764 Jan-25-2022, 11:46 AM
Last Post: korenron
  invalid syntax in my class CompleteNewb 2 1,909 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,175 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,264 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,763 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,289 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,335 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,393 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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