Questions tagged [f-string]

Literal string interpolation in Python

An f-string is the abbreviated name for a "formatted string" syntax that was introduced in Python 3.6 and extended in Python 3.7. They enable literal string interpolation:

>>> value = 80
>>> f'The value is {value}.'
'The value is 80.'

See PEP 498 for a comprehensive description of the feature, and also PEP 536 for the final grammar.

621 questions
0
votes
0 answers

Why does Netbeans show Python f-string as error?

(Note - this is a separate issue from one previously reported. The older report was Netbeans 7.x which did not yet support Python 3.x.) Netbeans with Python treats f-string as error. Netbeans IDE 8.0.2. Python project platform set Python 3.7.3. I…
L. Price
  • 11
  • 2
0
votes
1 answer

Problem with using dictionaries inside f string

I'm using f string format to do some printing. The interpreter throws a syntax error when I use a value from a dictionary. print(f'value = {mydict['key']}') Why is that, how can I overcome it?
Alex Deft
  • 2,531
  • 1
  • 19
  • 34
0
votes
0 answers

Writing decimal precision 0 but include decimal point

I need to write a decimal with 0/zero precision, but I want it to include the decimal point. I am using f-strings. This is what I have tried: test = f"{8:3.0f}" print(test) Out 8 As well as just doing: test = f"{8:3.0f}" + "." print(test) Out …
fallfish
  • 15
  • 6
0
votes
2 answers

How to nest a conditional 3 quote strings inside another 3 quote strings in python?

I am trying to a paragraph of lines using 3 quote strings where some group of lines in the paragraph are to be included on a if condition. I am using {} brackets for those conditional lines and since each of them have to be on the next line, I have…
Harshad Surdi
  • 23
  • 1
  • 6
0
votes
2 answers

In Python 3.6+ what is the f-string to print float 9.9 as string 09.90 and 10 as 10.00?

hHaving looked at three other formatting questions here with tens of answers each I don't see anyone telling how to print a float 9.9 as 09.90 and 10 as 10.00 using the same f-string: x = 9.9 print(f"{x:02.2f}") // should print 09.90 x =…
jbasko
  • 7,028
  • 1
  • 38
  • 51
0
votes
2 answers

Trying to understand some f-string magic (formatting mini language in f-strings)

In a comment on this post, somebody dropped this line of code: print("\n".join(f'{a:{a}<{a}}' for a in range(1,10))) 1 22 333 4444 55555 666666 7777777 88888888 999999999 And it looks like magic to me, can somebody explain to me why it works (more…
Benoît P
  • 3,179
  • 13
  • 31
0
votes
1 answer

SyntaxError when using literal string interpolation or f-strings

Trying to read a csv file and print contents: with open('C:\test.csv') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: print(f'Column names are {",…
0
votes
1 answer

Pythn 3.6 f-string on pre-formatted string

How can I re-format using f-string this string: string_from_db = "Hello {full_name}" full_name = "Mark X" return f'{string_from_db}' This example is not working, I have a string which is ready for params injections in the DB, And I want to use…
Oz Bar-Shalom
  • 1,747
  • 1
  • 18
  • 33
0
votes
1 answer

using f-string with C-style string formatting in python

I would like to write a function to build a bytes type string that need to use f-string with different value. the only way I can think about is like following code. anyone have better suggestion? In code, I have string like I have level but in my…
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
0
votes
1 answer

Name error with f-strings in python3

I am using the following f-strings in a web application. When I run it locally (before trying on the web) I am getting a name error: NameError: name 'path' is not defined But path prints properly and seems to be defined to my mind. What am I doing…
Woody Pride
  • 13,539
  • 9
  • 48
  • 62
0
votes
1 answer

python 3.6 - variable inside fstring with datetime function

I am trying the run below lines of code in a loop and want to define the timedelta as a variable as it will keep varying. START_TIME = f"{datetime.datetime.now() - timedelta(int('%d')):%d/%m/%Y 00:00}" % COUNT END_TIME = f"{datetime.datetime.now()…
Koshur
  • 378
  • 1
  • 6
  • 20
0
votes
1 answer

way to create fstring inside a fstring

is it possible to create a fstring inside a fstring? what i am trying now is to create a dynamic function using eval. my code is something like this func_string = '' for scenario in ['scene1', 'scene2']: for aa in ['int', 'ext']: …
user466130
  • 357
  • 7
  • 19
0
votes
1 answer

f-string python invalid sysntax

I recently trying to learn to code in python and I have zero knowledge in any programming languages our there and I encounter this problem in it, in which I am trying to use the f-string function. My Python version is 3.6.2 and here's the code that…
user8493251
0
votes
0 answers

Should we abandon %-formatting in profit of f-strings in Python?

With the latest Python 3.6, we have a new nice way to format strings, the f-strings: f'Hello {name}'. Let's assume we are starting a project supporting only Python >3.6, should we totally abandon the good old %-formatting ('Hello %s' % name), which…
frankie567
  • 1,703
  • 12
  • 20
0
votes
1 answer

How can i use f-string formatting to rewrite a statement:

I have read and re-read a handful of pages about PEP and literal stringinterpolation. But still can't figure out through experimentation what exact syntax will work so i can remove the %s from following statement in my python script.…
lpt
  • 1