Python Forum
Newbie on Python syntax - 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: Newbie on Python syntax (/thread-26112.html)



Newbie on Python syntax - rud0lp20 - Apr-21-2020

Hi guys,

Can you suggest a better documentation/site that can help me on syntax?
currently I have this issue where on windows its working but when I tried to run it on linux based I'm getting the syntax issue on this piece of code..

msg = f'| {pbar} {i*b_size}/{seq_len*b_size}| '
Python Version: 3.7.4
Full error:
Quote: File "main.py", line 3
msg = f'| {pbar} {i*b_size}/{seq_len*b_size}| '
^
SyntaxError: invalid syntax

TIA


RE: Newbie on Python syntax - bowlofred - Apr-21-2020

You need to post the full error message (using the error tags).

You should also list the version of python that you are running as well.


RE: Newbie on Python syntax - rud0lp20 - Apr-21-2020

(Apr-21-2020, 04:03 PM)bowlofred Wrote: You need to post the full error message (using the error tags).

You should also list the version of python that you are running as well.

ow sorry for that..ok..I will update main thread..:)


RE: Newbie on Python syntax - deanhystad - Apr-21-2020

f'' is the newest way to format a python string. Some time ago we used % and then we started using .format and now we have f''. I do have to admit I like f''.

Here is what it is doing.

The "f" in f'' is telling pithon that the string that follows is a special format string. In the format string {} is used to surround an expression. When the format string is converted to a string, the expression is evaluated and the (expression) is replaced by the result of the expression.

In your particular example there are going to be some variables named pbar, I, b_size, seq_len and b_size. When the format string is converted it will get the value of pbar, convert to a string, and use that in place of {pbar}. It will also evaluate i * b_size, convert that to a string, and use that in place of {i * b_size}.

The | and / are just normal characters that requie no conversion. I don't know the type for pbar, but I guess the output may look something like:
| pbarstr 10/3|

Those {} may also contain additional formatting information to control how many decimals are printed for a float, how many characters should be used to print an expression value, and how the value should be justified (left, right or center). Your example does not contain any special formatting.


RE: Newbie on Python syntax - rud0lp20 - Apr-21-2020

@deanhystad
ahhh..thank you..now I got it..:)


RE: Newbie on Python syntax - deanhystad - Apr-21-2020

Thanks for leaving me hanging. Honest, my lengthy reply actually made sense given the original post. Or did it only in my head?

Your format string works fine for me. My guess is your syntax error is elsewhere.

More info on string formatting is everywhere. But officially it is here:

https://docs.python.org/3.8/library/string.html


RE: Newbie on Python syntax - jefsummers - Apr-21-2020

Odds are the error is in the line above the line where it is flagged. Unmatched parentheses for example will generate such an error often on the next line. Pls post all (or at least a bigger segment) of your code