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

Any way to concatenate two strings and remain the quotes(Python)?

I wrote a function which includes a groupby aggregation. For example df.groupby([column, 'columnA', 'columnB', 'columnE'....'columnZ'].sum(), where column is the input variable. Since there are many groupby columns, I don't want to rewrite all of…
Jiamei
  • 405
  • 3
  • 14
0
votes
2 answers

Is there a way to have spaces in f-string?

I am trying to get the index for a value in a specific column of my DF that is variable and must have spaces in it. I do not have control of the name of this column. I'm using query with f-string. So lets say I have the following. import pandas as…
Jenn B
  • 1
  • 1
0
votes
0 answers

Trying to evaluate a string as an f-string results in AttributeError: 'A' object has no attribute 'f()'

I have a string that contains {...} f-string elements that I'd like to get evaluated. And How to evaluate a variable as an f-string? tells me to do this: >>> class A: ... def f(self): ... return "something" ... >>> a = A() >>>…
finefoot
  • 9,914
  • 7
  • 59
  • 102
0
votes
1 answer

In Matplotlob writing mathematical expressions with f-literals; how to deal with the raise to the power (^)

I want to display certain equations on a Matplotlob plot. Following Writing mathematical expressions with Matplotlob I wrote the following. The coefficients for the question are also only available at runtime, so I also use python f-string literals.…
KarateKid
  • 3,138
  • 4
  • 20
  • 39
0
votes
0 answers

Multiple dollar signs in my annotated string in matplotlib

I've searched all over and can't find a strait forward answer to this question. I tried ax.text as well instead of annotate and still couldn't get it to work. I simply want to print my string EXACTLY as I typed it in the code, but matplotlib keeps…
TER
  • 107
  • 6
0
votes
1 answer

How to mix f-string and LaTeX symbols in a print(...) statement?

I am calculating pi with the Monte Carlo "quarter circle" method, using the following program: from random import random as rd def estimPi(n_points): counter = 0 for i in range(n_points): x,y = rd(),rd() if x**2 + y**2 < 1: …
Andrew
  • 926
  • 2
  • 17
  • 24
0
votes
0 answers

Hello everyone, I need help to align a f-string print. Whenever I try, the result is never as expected

In the code below I'm trying to print the keys and values of a list aligned, but I don't know why it doesn't work as expected! for k, v in enumerate(time): print(f'{k : >5}', end=' ') for d in v.values(): print(f' {str(d): <17}', end='') …
RenatoP
  • 1
  • 1
0
votes
2 answers

For every run how to change the item being called using f-strings

So I'm trying to make so everytime the for loop runs, it changes slightly the driver.get() function, so i can shorten my code instead of typing driver1.get, driver2.get driver3.get I tried to things and neither work: from selenium import…
0
votes
0 answers

How to round fractional coefficient with f-string

After carrying a Chi2 test on some contingency table (using: scipy.stats.chi2_contingency(myTable)) I get some statistics: stat=215.87297677691768 p=1.6223014100068392e-27 dof=36 I then print these results using the f-string formatting: new_line =…
Andrew
  • 926
  • 2
  • 17
  • 24
0
votes
2 answers

In Python how do you make the variable in an f'-string a range of numbers which are inserted and run one by one

I'm pretty new to the coding world, so maybe someone can help me. Might even be a straight forward problem. Currently I'm using selenium and python for a project, but I can't find anything helpful so far. Would it be possible to define the variables…
Krischk
  • 35
  • 6
0
votes
1 answer

What method does f-string use to display an instance of a class?

Consider: class Base: def __init__(self): self.__repr__ = lambda: "String!" class Child(Base): pass c = Child() print(f"{c}") print(f"{c.__repr__()}") This results in: <__main__.Child object at 0x7f07cd88f850> String! I get the…
AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
0
votes
0 answers

Using f-strings to format class attribute

I have this class defined: class Endpoint: recent_releases = "apps/{owner_name}/{app_name}/recent_releases" recent_latest = "apps/{owner_name}/{app_name}/releases/{release_id}" and currently using format() to format it: url =…
FrancisV
  • 1,619
  • 5
  • 21
  • 36
0
votes
1 answer

F String Returns on new line

I am building a song metadata library and the f string keeps returning a new line on the BPM String I added: def all(self): return '{} {} {} {} {} {} {} {} {} {} {}'.format(self.title, self.length, self.scale, …
0
votes
1 answer

How do you create a new line in f-strings?

I'm trying to create new lines using python for the variable "profile" using the 'new_line' variable, but I haven't been successful. The code below does not produce errors. It gives me all 3 strings in one line and I'd like to get 3 lines. I would…
0
votes
0 answers

Python: Extracting parameters of f-String from a log

I have an log-file with some written f-strings. Now I want to get the parameters again and save them in a dictionary. e.g. I have the following log: The following data is received: Name: Kreijeck, age=35, country=Germany from this method: def…