Python Forum
Invalid syntax with an f-string - 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: Invalid syntax with an f-string (/thread-36062.html)



Invalid syntax with an f-string - Mark17 - Jan-13-2022

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



RE: Invalid syntax with an f-string - deanhystad - Jan-13-2022

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}")



RE: Invalid syntax with an f-string - Mark17 - Jan-13-2022

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


RE: Invalid syntax with an f-string - snippsat - Jan-14-2022

(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



RE: Invalid syntax with an f-string - BashBedlam - Jan-14-2022

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



RE: Invalid syntax with an f-string - Mark17 - Jan-14-2022

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


RE: Invalid syntax with an f-string - snippsat - Jan-14-2022

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)


RE: Invalid syntax with an f-string - Mark17 - Jan-14-2022

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