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

Guidance with f-string formatting documentation

I'm currently playing around with f-strings more and trying to figure out how to do things like # Depth and decimal format f"string text {var: #.#f}" # Alignment format f"string text {var: < #}" I know that is how you would do a depth, decimal,…
0
votes
1 answer

Float formatting with f-string using a class attribute as parameter

Is it possible to do something like this? class P(): def __init__(self): self.digits = 5 self.pi = f"{np.pi:eval(self.digits)f}" test = Test() test.pi I know that f"{np.pi:5.f}" works, but is it possible to do it with the…
0
votes
2 answers

Unpacking Comprehensed list without " ' " in f strings

I'm trying to make a sql query with list comprehension. First I take a column names like this COLUMNS_ONE = ["id INT(6)", "firstname VARCHAR(30)"] COLUMN_NAMES_ONE = [name.split(" ")[0] for name in COLUMNS_ONE] print(COLUMN_NAMES_ONE) >>> ["id",…
Marcel Kopera
  • 304
  • 2
  • 9
0
votes
1 answer

What does this string format mean: (return f"${value:,.2f}")

I'm working on cs50's web track Finance Project, and in their helpers.py file they have the following function: def usd(value): """Format value as USD.""" return f"${value:,.2f}" I believe that it takes a value and transforms into USD…
Nicolas F
  • 505
  • 6
  • 17
0
votes
5 answers

f-strings with variable number of parameters?

I need to create messages of the kind Hi John, it's time to eat quantity_1 of food1, quantity_2 of food_2 ..., qunatity_n of food_n. For that I get pandas dataframes, that update every once in a while. For example the dataframes look sometimes like…
NeStack
  • 1,739
  • 1
  • 20
  • 40
0
votes
1 answer

Options to replace Python F string

I'm trying to use YoutubeAPI to get the URLs from each video within a playlist. I found some code to help me do this, however, they use a print statement with an F string and a for loop that iterates over each video in the playlist. The problem is,…
moistCash
  • 29
  • 5
0
votes
1 answer

How do I display LaTeX braces in a f-string in Matplotlib

I'm unable to get LaTeX braces to display in my Matplotlib figures when I create labels using f-strings. For example fig, ax = plt.subplots(1, 3, figsize=(12,4), sharey=True) fig.suptitle("$x_n = x_{n-1}^2$, " + f"$x_0={5:.2f}, \,r={6:.2f}, \,n \in…
orome
  • 45,163
  • 57
  • 202
  • 418
0
votes
3 answers

f-strings multi-line printing wrong order

I want to print out a report with the following python (3.8) code, but you can see that the pandas info prints at the top instead of the bottom. I can't seem to find any solutions to this odd behavior. Any ideas for how to fix this? time = 600 file…
BlueFruit
  • 21
  • 3
0
votes
1 answer

Python3 f string vs .replace

I have a text file where most of the text is boilerplate, with around two dozen variables that I'm changing in Python depending on the room that the file pertains to. Which method of replacing the text is "better", wrapping the entire text file into…
logan
  • 55
  • 6
0
votes
0 answers

How to use self-docummenting equals (debugging) specifier with str.format()?

Python 3.8 introduced = specifier in f-strings (see this issue and pull request). It allows to quickly represent both the value and the name of the variable: from math import pi as π f'{π=}' # 'π=3.141592653589793' I would like to use this feature…
krassowski
  • 13,598
  • 4
  • 60
  • 92
0
votes
2 answers

How to restrict the number of digits in an fstring in Python

I would like to use an f-string in Python to generate the following folders: folder01 folder02 folder03 folder04 folder05 folder06 folder07 folder08 folder09 folder10 folder11 I currently have the following code, which obviously doesn't do what I…
An Ignorant Wanderer
  • 1,322
  • 1
  • 10
  • 23
0
votes
1 answer

Trying to use any() to change a word in a url

I am trying to create a program that takes user input of a suburb and then returns a list of good cafes in that suburb. The webpage I am scraping has a number of good cafe lists for various suburbs but has not got a list for every suburb where I…
deadant88
  • 920
  • 9
  • 24
0
votes
1 answer

Python: How can I evaluate an f-string after it is created

I'd like to evaluate an f-string that I have queried from a database. This string uses a variable I have defined before calling the string. def get_name(): name = "Ben" # from database (the value within the text field I call "name_question"…
Jaco
  • 1,564
  • 2
  • 9
  • 33
0
votes
1 answer

Python Expression that returns a value if condition is met but otherwise continues a for loop

This is going to sound like a dumb and horrible idea and that's probably because it is. Is there a way in Python (preferably a one-liner) to create an expression that resolves to a value if a condition is met but if the condition is not met it will…
0
votes
1 answer

How does the f-string in this Python function work?

Hi I found the below function on some website somewhere and just have a couple of questions. The function returns a diamond of n lines made from asterisks. Is that a concatenated for loop? Is that a thing you can do? What is going on in that…