Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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, this is regarding "*" or_expr part of the following BNF.

f_string          ::=  (literal_char | "{{" | "}}" | replacement_field)* replacement_field ::=  "{" f_expression ["!" conversion] [":" format_spec] "}" f_expression      ::=  (conditional_expression | "*" or_expr)                          ("," conditional_expression | "," "*" or_expr)* [","]                        | yield_expression 

I tried it in REPL, but it causes an error.

>>> l = [1, 2, 3] >>> f"{l}" '[1, 2, 3]' >>> f"{*l}"   File "<stdin>", line 1 SyntaxError: can't use starred expression here 
like image 907
Takahiro Kobayashi Avatar asked May 01 '19 09:05

Takahiro Kobayashi


People also ask

What two symbols does an F-string work?

The f-strings have the f prefix and use {} brackets to evaluate values. Format specifiers for types, padding, or aligning are specified after the colon character; for instance: f'{price:. 3}' , where price is a variable name.

What does an asterisk do in a string Python?

The asterisk (star) operator is used in Python with more than one meaning attached to it. Single asterisk as used in function declaration allows variable number of arguments passed from calling environment. Inside the function it behaves as a tuple.

What does F in front of a string do?

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.

Can you use an F-string in a variable?

If you have multiple variables in the string, you need to enclose each of the variable names inside a set of curly braces. The syntax is shown below: f"This is an f-string {var_name} and {var_name}." ▶ Here's an example.


1 Answers

There are two alternatives for f_expression: a comma separated list of or_exprs, optionally preceded by asterisks, or a single yield_expression. Note the yield_expression does not allow an asterisk.

I assume the intention was that the comma-separated list alternative is only chosen when there's at least one comma, but the grammar doesn't actually say that. I feel like the repetition operator at the end should have been a + instead of a *.

So f"{*1}" would be a syntax error because there's an asterisk, but no comma. f"{*1,*2}" is syntactically valid, but a type error because 1 and 2 aren't iterable. f"{*[1], *[2]}" is valid and acts the same as f"{1,2}". So the asterisk is allowed because it acts as the splat operator in tuples, which can be written without parentheses in f-expressions.

Note that using or_expr as the operand to * does not mean that a bitwise or-operator has to be used there - it just means that the bitwise or-operator is the first operator in the precedence-hierachy that would be allowed as an operand to *. So it's just about setting the precedence of prefix * vs. other expressions. I believe or_expr is consistently used as the operand to prefix * everywhere in the grammar (that is, everywhere where prefix * is followed by an expression as opposed to a parameter name).

like image 104
sepp2k Avatar answered Oct 26 '22 19:10

sepp2k