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
5
votes
2 answers

How to handle formatting a regular string which could be a f-string [C0209]

For the following line: print("{0: <24}".format("==> core=") + str(my_dict["core"])) I am getting following warning message: [consider-using-f-string] Formatting a regular string which could be a f-string [C0209] Could I reformat it using…
alper
  • 2,919
  • 9
  • 53
  • 102
5
votes
1 answer

Python - Is there a shorthand for [eg]: print(f'type(var) = {type(var)}')

Is there a shorthand in Python for (e.g.) print(f'type(var) = {type(var)}'), without having to state the object in the text and the {.}? The short answer may be "no", but I had to ask! E.g. in SAS one may use &= to output a macro variable and its…
5
votes
2 answers

How do I add curly brackets to f-strings in Python 3?

I have this code class = "maximum" s = f"""The code for {class} is {3854-st56}""" print(s) I want this to output: >> The code for maximum is {3854-st56} But the f-string doesn't recognize the curly brackets around 3854-st56 and instead thinks I am…
Vidit Gautam
  • 97
  • 1
  • 5
5
votes
2 answers

Why does this print statement using a Python f-string output double parentheses?

In the following code I output the range of values which I can use to index the list lst. However, due to some oddity of Python's format literals the output contains two opening and two closing parentheses instead of one. I don't see what's wrong…
5
votes
1 answer

How to prettyprint f-Strings?

Attempting to pprint a dictionary from an f-String: import pprint as pp my_dict = { "key_a": ["here", "are", "the", "things"] , "key_b": ["that", "i", "want"] , "key_b": ["to", "share", "with", "the",…
Kermit
  • 4,922
  • 4
  • 42
  • 74
5
votes
2 answers

Insert newline after equals sign in self documenting f-string

With python3.8, a new feature is self documenting format strings. Where one would normally do this: >>> x = 10.583005244 >>> print(f"x={x}") x=10.583005244 One can now do this, with less repetition: >>> x = 10.583005244 >>>…
Enigma Machine
  • 123
  • 1
  • 7
5
votes
1 answer

Should I use f-strings?

These two print statements produce the same results and at least to me the first version looks more readable. Should I stick with the f' version because it will cause issues for me later on or cause worse performance or does not follow the present…
KTFLash
  • 63
  • 1
  • 5
5
votes
1 answer

f-strings formatter including for-loop or if conditions

How can I insert for loops or if expressions inside an f-string? I thought initially of doing something like this for if expressions: f'{a:{"s" if CONDITION else "??"}}' What I would like to do though is something like: Example 1 f'{key: value\n…
dearn44
  • 3,198
  • 4
  • 30
  • 63
5
votes
3 answers

Rounding float using f-string

Using %-formatting, I can round the number of decimal cases in a string: pi = 3.14159265 print('pi = %0.2f' %pi) And in output(in terminal) this would give me: pi = 3.14 Can I use f-strings do this task? This feature has been added in Python 3.6
Heyl
  • 53
  • 1
  • 4
5
votes
3 answers

Print self-documenting expression with value of variable on a new line

Python 3.8 allows for self-documenting expressions and debugging using =, e.g.: print(f'{myvar=}') Is it possible to print the output on a new line? This would be useful for variables with multi-line outputs like dataframes, e.g. import pandas as…
John
  • 949
  • 1
  • 9
  • 20
5
votes
4 answers

Python 3 f-string alternative in Python 2

Mostly I work with Python 3. There can I write this: print(f"The answer is {21 + 21}!") Output: The answer is 42! But in Python 2, f-strings do not exist. So is the following the best way? print("the answer is " + str(21 + 21) + "!")
Marius Illmann
  • 302
  • 3
  • 5
  • 14
5
votes
2 answers

Trigger f-string parse on python string in variable

This question comes from handling jupyter magics, but can be expressed in a more simple way. Given a string s = "the key is {d['key']}" and a dictionary d = {'key': 'val'}, we want to parse the string. The old method would be .format(), which will…
Roelant
  • 4,508
  • 1
  • 32
  • 62
5
votes
2 answers

Leverage Python f-strings with Yaml files?

If I have a yaml file containing a string with the bracket notation {} used in partnership with python f-strings, how might a leverage the f-string interpolation here? Take for example this simple yaml file: # tmp.yaml k1: val1 k2: val2 as well as…
user9074332
  • 2,336
  • 2
  • 23
  • 39
4
votes
1 answer

Replace value of variable directly inside f-string

I'm trying to replace a value inside a variable that I use within an f-string. In this case it is a single quote. For example: var1 = "foo" var2 = "bar '" print(f"{var1}-{var2}") Now, I want to get rid of the single quote within var2, but do it…
JVGBI
  • 565
  • 1
  • 8
  • 26
4
votes
3 answers

Can f-strings auto-pad to [the next] even number of digits on output?

Based on this answer (among others) it seems like f-strings is [one of] the preferred ways to convert to hexadecimal representation. While one can specify an explicit target length, up to which to pad with leading zeroes, given a goal of an output…
Adam Smooch
  • 1,167
  • 1
  • 12
  • 27