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
1 answer

f-string and multiprocessing.Manager dict and list

I am writing this question, because it does not seem like what I am experiencing should be the desired behavior: The following code is pretty much lifted from the 3.9.16 documentation on multiprocessing.Manager from multiprocessing import Process,…
tipanverella
  • 3,477
  • 3
  • 25
  • 41
0
votes
2 answers

Converting a list of strings into part of a f string in Python

I have a list of error messages: errors = [ "this is an error", "this is another error", ] I am then sending this as an email using Amazon SES: ses_client.send_email( Destination={ "ToAddresses": [ …
user7692855
  • 1,582
  • 5
  • 19
  • 39
0
votes
0 answers

Formatting while using "=" syntax in f-string python

If there is a variable var=0.00012646547, and it is being printed using the "=" syntax feature provided by f-string as print(f'{var=}') , is there a way to format the output which would provide a similar result as the following code…
0
votes
0 answers

Python: forcing imported string interpolation for SPARQL (not SQL)

I use f-strings to build SPARQL queries with a variable and they work well as follows: for label in longnames: sparqlquery = f""" PREFIX osr: SELECT * WHERE {{ ?uri a osr:Senatore. ?uri…
Robert Alexander
  • 875
  • 9
  • 24
0
votes
1 answer

Python f string curly braces

new to python and programming in general. Forr the following code, why would 'a''b''c''d' be printed separately for each loop, I understand that 'abcd' would be treated as an array but does putting it in curly braces have a special meaning? Is this…
duckpro
  • 17
  • 2
0
votes
1 answer

How to format a formatted string in Python? Either with .format or f-string

I have a string (sql query) in which I want to pass a variable at one point, then pass another variable at another point (list of variables, but just focusing on one for now). The expected would be something like this: sql_query = 'SELECT {{field}}…
EAA
  • 87
  • 1
  • 9
0
votes
0 answers

SPARQLWrapper query error with an f-string : QueryBadFormed

I am trying to lookup the names of Italian Senators in a public SPARQL endpoint. Starting sample data is the following set: longnames = ['Lars Danielsson', 'Giorgia Meloni', 'Ursula von der Leyen', 'Filippo Mannino', 'Matteo Piantedosi', 'Lamberto…
Robert Alexander
  • 875
  • 9
  • 24
0
votes
0 answers

Disallow passing f-strings as argument

I am reporting data from some tests, and each test can have a 'summary' and a 'details' section. The 'summary' field for any particular test should be static, with any additional dynamic information going into the details field, as…
tigerninjaman
  • 393
  • 3
  • 17
0
votes
2 answers

What is wrong f-string in Python?

below code is my valid requirement. import numpy as np a =np.array([1, 3, 0, 2], int) b =np.array([5, 2, 1, 2], int) print(f'{a > b = }') result is ok a > b = array([False, True, False, False]) I wanna make inner function which have…
dEitY719
  • 1
  • 1
0
votes
1 answer

format fstring with decimal and aligned places

(https://i.stack.imgur.com/a5RxM.png) Is there any way to format the variable space media and make it 2 decimal places at the same time? I want to keep the variable aligned.
Alan
  • 1
  • 1
0
votes
2 answers

ValueError: Invalid format specifier when using f-strings inside a function

I am trying to format a pandas series: for all values, I want to add thousands separators and limit decimal places to two. In addition, for negative values, instead of a - sign, I want to enclose it with parentheses. So output should look like…
Bowen Liu
  • 1,065
  • 1
  • 11
  • 24
0
votes
1 answer

Right justify an f-string with multiple columns of data

Help me!! It's not justified (python) # Accept the inputs startBalance = float(input("Enter the investment amount: ")) years = int(input("Enter the number of years: ")) rate = int(input("Enter the rate as a %: ")) # Convert the rate to a decimal…
0
votes
2 answers

F-String Literal Not Printing kwargs

def myfunc (*args, **kwargs): print (args) print (kwargs) print (f'I would like {args[0]} {kwargs['food']}') #Example #2 print ('I would like {} {}'.format(args[0], kwargs['food'])) myfunc(10,20,30,fruit = 'orange', food = 'eggs',…
0
votes
0 answers

Pulp: Minimization; Sum product within lpSUM

I have 20 components and 5 models that I am working with. Using the inventory of component in stock I am trying to identify what additional inventory to order and which models to build in order to minimise inventory. #Importing libraries import…
0
votes
1 answer

f-string like behaviour with explicit format method

With f-string I can do like this: a = 10 f'a equals {a}' # 'a equals 10' f'b equals {a - 1}' # 'b equals 9' but when using .format I cannot do any operation on the variable: 'b equals {a - 1}'.format(dict(a=10)) # KeyError: 'a - 1' The error…
hans
  • 1,043
  • 12
  • 33