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

hook into the builtin python f-string format machinery

Summary I really LOVE f-strings. They're bloody awesome syntax. For a while now I've had an idea for a little library- described below*- to harness them further. A quick example of what I would like it do: >>> import simpleformatter as sf >>> def…
Rick
  • 43,029
  • 15
  • 76
  • 119
27
votes
3 answers

What does a star (asterisk) do in f-string?

In the python document 2.4.3. Formatted string literals, it seems possible to write a star followed by an expression in a f-string's {}, but I cannot find how to use that. What's that and how I can use it? Is it documented somewhere? To be exact,…
26
votes
6 answers

Is it possible to format strings in C++ like python's f-strings?

In Python it's possible to format strings conveniently using f-strings: a = 42 print(f"a = {a}") # prints "a = 42" Is it possible to do something like this in C++ at compile time, or is format("a = {}", a); the closest thing to f-strings? EDIT: I'm…
Bamberghh
  • 429
  • 1
  • 5
  • 8
26
votes
5 answers

String concatenation with + vs. f-string

Let's say I have two variables: >>> a = "hello" >>> b = "world" I can concatenate them in two ways; using +: >>> a + b "helloworld" Or using an f-string: >>> f"{a}{b}" "helloworld" Which way is better or a better practice? Someone told me the…
JamesWang
  • 1,175
  • 3
  • 14
  • 32
26
votes
2 answers

How to pass string format as a variable to an f-string

I am using f-strings, and I need to define a format that depends upon a variable. def display_pattern(n): temp = '' for i in range(1, n + 1): temp = f'{i:>3}' + temp print(temp) If it is relevant, the output of…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
22
votes
1 answer

Dictionary/set comprehensions inside of f-string

Is it possible to have a dictionary or set comprehension inside of an f-string in python 3.6+? It seems syntactically impossible: names = ['a', 'b', 'c'] pks = [1, 2, 3] f"{{name : pk for name, pk in zip(names, pks)}}" This will return: {name :…
21
votes
2 answers

How would I define a fill character for f-string alignment?

I've been messing around with text-alignment, including the string methods, and f-strings. My current goal is to create a table of contents styled output, that looks something like this: Introduction....................1 Python…
PYer
  • 458
  • 3
  • 12
21
votes
1 answer

Why are f-strings faster than str() to parse values?

I was playing around with f-strings (see PEP 498), and I decided to check the speed of the f-string parse, (e.g. f"{1}") in comparison with the usual str parse (e.g str(1)). But to my surprise, when I checked the speed of both methods with the…
Davichete
  • 415
  • 7
  • 14
20
votes
4 answers

Python f-string formatting not working with strftime inline

I'm hitting an odd error that I'm trying to understand. Doing some general code cleanup and converting all string formatting to f-strings. This is on Python 3.6.6 This code does not work: from datetime import date print(f'Updated…
Sam Morgan
  • 2,445
  • 1
  • 16
  • 25
20
votes
4 answers

Why isn't it possible to use backslashes inside the braces of f-strings? How can I work around the problem?

In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent: '{} {}'.format(2+2, "hey") f'{2+2} {"hey"}' Disregarding format specifiers, I can basically move the positional arguments…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
20
votes
2 answers

Avoiding None in f-string

If I have an f-string where one of the parameters may be None, is there a way to automatically omit it from the string? f'{first_name} {prefix} {last_name}' If prefix is None then this string will render as Arnold None Weber. I tried f'{first_name}…
voodoo-burger
  • 2,123
  • 3
  • 22
  • 29
19
votes
5 answers

How to format a float with a comma as decimal separator in an f-string?

For some machine control in python, I write the results to a text-file, that someone else can copy into Excel (this is the most convenient way in this situation). However, in the Netherlands, Excel has a comma as decimal separator and thus I want to…
seaver
  • 351
  • 1
  • 2
  • 8
19
votes
3 answers

What are formatted string literals in Python 3.6?

One of the features of Python 3.6 are formatted strings. This SO question(String with 'f' prefix in python-3.6) is asking about the internals of formatted string literals, but I don't understand the exact use case of formatted string literals. In…
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
18
votes
1 answer

What does !r mean in Python?

I have found the following code in a project. What does the !r part mean? def __repr__(self): return f"user={self.user!r}, variant={self.variant!r}"
Nepo Znat
  • 3,000
  • 5
  • 28
  • 49
18
votes
2 answers

Why doesn't f-strings formatting work for Pandas DataFrames?

Given a DataFrame with Product Id and Amount: df = pd.DataFrame([['504145', 12000.0], ['555933', 23010.5]], columns=['Product Id', 'Amount']) df Out[1]: Product Id Amount 0 504145 12000.0 1 555933 …
henrywongkk
  • 1,840
  • 3
  • 17
  • 26
1 2
3
41 42