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 not formatting floats into an integer

Usually, I use % string formatting. But, as I discovered f-string is the new way of string formatting and it is faster as well, I want to use it. But, I am facing a problem when formatting a float into an integer using f-string. Following is what I…
Satish Thorat
  • 584
  • 1
  • 5
  • 13
0
votes
0 answers

f-string padding issue with idle

i was printing some simple text and after a while the padding got funky i have tried changing the text size and font in idle to no avail i have also tried running it in the terminal which did print the text correct for root, dirs, files in…
Willes05
  • 1
  • 1
0
votes
1 answer

How to create f-string new line within a list?

I am trying to use f-string within a list. I have created a new line variable. Standard variables work fine but the new line variable does not NL = '\n' var = 'xyz' lst = [] print(f'test{NL}new line') lst.append(f"first line var is {var}{NL}a…
Mick Sulley
  • 99
  • 2
  • 11
0
votes
1 answer

Using f-string inside a method

I have code that looks like this: from dataclasses import dataclass @dataclass class Example: a: str b: str c: str def repeated_code(self): if self.c == 'A': if self.a != "": print(f"as…
David Masip
  • 2,146
  • 1
  • 26
  • 46
0
votes
2 answers

How to avoid creating a newline when using if-else in f-string expression

Please see the below minimal example, printbbb = True print(f"""AAA {'''BBB''' if printbbb else ''} CCC""") This prints AAA BBB CCC as desired, however, if I set printbbb to False, it prints AAA CCC How can I change the code so that it will…
user3667089
  • 2,996
  • 5
  • 30
  • 56
0
votes
0 answers

How to add thousands separator and without it shown as scientific notation

When I add a thousands separator, Python prints the number as scientific notation; but I want it to show the full number. winter_s = (ls2016.loc[(ls2016.month == 12) | (ls2016.month == 1) | (ls2016.month == 2)]).index spring_s =…
0
votes
1 answer

Paramspace with MissingInputException despite correct expansion

My workflow is approximately as follows: import pandas as pd from snakemake.utils import Paramspace TEMP_DIR = "temp" chr = 22 # Set up paramspace for QC values: paramspace = Paramspace(pd.read_csv("config/qc_values.tsv", …
Giulio Centorame
  • 678
  • 4
  • 19
0
votes
2 answers

Facing issue KeyError while formatting string which contains JSON

In below code I'm facing KeyError, even though my syntax seems fine. data = {'id': 20720} query =…
Shiva
  • 3
  • 1
0
votes
1 answer

Python function not working properly with f'{col}'

EDIT SUMMARY So I have: p1 dataframe with the prices and the returns of n-financial instrument of the last 5 years: Date a_price b_price a_ret b_ret 0 2018-04-13 6.335 5.114 0.0047 0.01 . . …
CristinaK
  • 23
  • 3
0
votes
1 answer

How to format f-strings from dataframe with curly brackets in the dataframe?

How to format f-strings from dataframe with curly brackets in the dataframe? Text in a dataframe that has curly brackets { and } in the text, which is intended to trigger f-string formatting of defined variables in the Python code. But the code…
user1574881
  • 91
  • 1
  • 6
0
votes
1 answer

What causes the difference between `f"{a}"` vs `f"{a=}"` in python's f strings?

In the following python code, what causes the difference in outputs? try: open('nonexistent') except Exception as err: print(f'{err=}') print(f'{err}') result: err=FileNotFoundError(2, 'No such file or directory') [Errno 2] No such file…
extremeaxe5
  • 723
  • 3
  • 13
0
votes
1 answer

Python :.2f strange behavior

I had experimented with some code on Python and I got strange results with :.2f. >>> a = 4.34567 >>> a 4.34567 >>> f"{a:.2f}" '4.35' >>> "%.2f" % a '4.35' >>> "{:.2f}".format(a) '4.35' > I expected results like: >>> a = 4.34567 >>> a 4.34567 >>>…
fmmaks
  • 1
  • 2
0
votes
2 answers

Python logging - Is there a way to not execute functions which are in strings, when the message is not going to be printed anyways due to logLevel?

I have some pretty long functions which are only used in debugging, but they execute while running in higher loglevels, even though I don't want them to. I understand that I can use if logger.getEffectiveLevel() to bypass this, but is there a…
0
votes
1 answer

How to display required amount of leading zeroes with Python3 f-string?

I just want to format a number as hex, with four characters in all - so up to three leading zeroes. I'm trying this: $ python3 --version Python 3.10.10 $ python3 -c 'TVAR=0x000a; print(f"{TVAR:#04x}")' 0x0a So, I thought that with 04x I had…
sdbbs
  • 4,270
  • 5
  • 32
  • 87
0
votes
2 answers

Using an f-string to combine an int and a float to a column in Pandas

I have a dataframe with minutes as an int in one col and seconds as a float in another column. I haven't been able to combine them so I want to create an object col to display them where I use an f-string to concatenate the minutes with the seconds…