Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie on Python syntax
#1
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
Reply
#2
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.
Reply
#3
(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..:)
Reply
#4
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.
Reply
#5
@deanhystad
ahhh..thank you..now I got it..:)
Reply
#6
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
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newbie laleebee 2 1,331 May-24-2022, 01:39 PM
Last Post: laleebee
  python newbie marcush929 2 2,608 Jan-01-2020, 08:06 AM
Last Post: marcush929
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,640 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Python Linting (newbie) LieveHeer 2 2,634 Jan-24-2019, 05:36 PM
Last Post: LieveHeer
  Python newbie is asking Marcus_Gondwe 4 3,436 Apr-04-2018, 11:08 AM
Last Post: Marcus_Gondwe
  Newbie ? Python 3 Input() jlgrunr 1 2,470 Feb-17-2018, 10:26 PM
Last Post: Gribouillis
  Python Newbie phylon 1 31,802 Jan-09-2018, 10:46 AM
Last Post: Gribouillis
  NEWBIE error regarding syntax rohitn12 1 2,344 Dec-19-2017, 10:19 PM
Last Post: micseydel
  NEWBIE HELP: How to tell Python when to use a function BoaVenom18 4 5,438 Dec-17-2016, 09:00 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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