Why were literal formatted strings (f-strings) so slow in Python 3.6 alpha? (now fixed in 3.6 stable)

Note: This answer was written for the Python 3.6 alpha releases. A new opcode added to 3.6.0b1 improved f-string performance significantly. The f”…” syntax is effectively converted to a str.join() operation on the literal string parts around the {…} expressions, and the results of the expressions themselves passed through the object.__format__() method (passing any :.. … Read more

How to use gettext with python >3.6 f-strings

‘Hey {},’ is contained in your translation dictionary as is. If you use f’Hey {username},’, that creates another string, which won’t be translated. In that case, the format method remains the only one useable, but you could approach the f-string features by using named parameters _(‘Hey {username},’).format(username=username) or if you have a dictionary containing your … Read more

What does = (equal) do in f-strings inside the expression curly brackets?

This is actually a brand-new feature as of Python 3.8. Added an = specifier to f-strings. An f-string such as f'{expr=}’ will expand to the text of the expression, an equal sign, then the representation of the evaluated expression. Essentially, it facilitates the frequent use-case of print-debugging, so, whereas we would normally have to write: … Read more

Can I import Python’s 3.6’s formatted string literals (f-strings) into older 3.x, 2.x Python?

future-fstrings brings f-strings to Python 2.7 scripts. (And I assume 3.3-3.5 based on the documentation.) Once you pip install it via pip install future-fstrings, you have to place a special line at the top of your code. That line is: # -*- coding: future_fstrings -*- Then you can use formatted string literals (f-strings) within your … Read more

How can I use f-string with a variable, not with a string literal?

f”…” strings are great when interpolating expression results into a literal, but you don’t have a literal, you have a template string in a separate variable. You can use str.format() to apply values to that template: name=[“deep”,”mahesh”,”nirbhay”] user_input = “certi_{element}” # this string i ask from user for value in name: print(user_input.format(element=value)) String formatting placeholders … Read more

String with ‘f’ prefix in python-3.6

See PEP 498 Literal String Interpolation: The expressions that are extracted from the string are evaluated in the context where the f-string appeared. This means the expression has full access to local and global variables. Any valid Python expression can be used, including function and method calls. So the expressions are evaluated as if they … Read more

f-string representation different than str()

From “Formatted string literals” in the Python reference: f-strings are invoke the “format protocol”, same as the format built-in function. It means that the __format__ magic method is called instead of __str__. class Foo: def __repr__(self): return “Foo()” def __str__(self): return “A wild Foo” def __format__(self, format_spec): if not format_spec: return “A formatted Foo” return … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)