f-string

How to evaluate a variable as a Python f-string

瘦欲@ 提交于 2020-05-29 06:13:11
问题 I would like to have a mechanism which evaluates an f-string where the contents to be evaluated are provided inside a variable. For example, x=7 s='{x+x}' fstr_eval(s) For the usage case I have in mind, the string s may arise from user input (where the user is trusted with eval ). While using eval in production is generally very bad practice, there are notable exceptions. For instance, the user may be a Python developer, working on a local machine, who would like to use full Python syntax to

Why is this usage of python F-string interpolation wrapping with quotes?

断了今生、忘了曾经 提交于 2020-05-29 05:07:06
问题 Code in question: a = 'test' # 1) print(f'{a}') # test # 2) print(f'{ {a} }') # {'test'} # 3) print(f'{{ {a} }}') # {test} My question is, why does case two print those quotes? I didn't find anything explicitly in the documentation. The closest thing I found detailing this was in the PEP for this feature: (the grammar for F-strings) f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... ' The expression is then formatted using the format protocol, using

f-strings formatter including for-loop or if conditions

时间秒杀一切 提交于 2020-05-14 09:48:28
问题 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 for key, value in dict.items()}' result: if dict = {'a': 1, 'b': 2} a: 1 b: 2 or Example 2 c = 'hello' f'{c} {name if name else "unknown"}' result: if name exists, e.g. name = 'Mike' hello Mike otherwise hello unknown Can this be done and if yes how?

f-strings formatter including for-loop or if conditions

纵饮孤独 提交于 2020-05-14 09:47:06
问题 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 for key, value in dict.items()}' result: if dict = {'a': 1, 'b': 2} a: 1 b: 2 or Example 2 c = 'hello' f'{c} {name if name else "unknown"}' result: if name exists, e.g. name = 'Mike' hello Mike otherwise hello unknown Can this be done and if yes how?

How to split up a long f-string in python?

喜你入骨 提交于 2020-05-12 11:18:18
问题 I am getting a line too long pep8 E501 issue. f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}' I tried using a multi-line string, but that brings in a \n , which breaks my test: f'''Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}''' How can I keep it single line and pass pep8 linting 回答1: You will need a line break unless you wrap your string within parentheses. In this case, f will need to

How to split up a long f-string in python?

为君一笑 提交于 2020-05-12 11:18:07
问题 I am getting a line too long pep8 E501 issue. f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}' I tried using a multi-line string, but that brings in a \n , which breaks my test: f'''Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}''' How can I keep it single line and pass pep8 linting 回答1: You will need a line break unless you wrap your string within parentheses. In this case, f will need to

String concatenation with + vs. f-string

99封情书 提交于 2020-04-30 06:24:47
问题 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 f-string is better practice in terms of performance and robustness, and I'd like to know why in detail. 回答1: There are two aspects to this: performance and convenience. Using timeit in Python 3.8.0, I get that concatenation using an f-string is

How can I parse Python's triple-quote f-strings?

泪湿孤枕 提交于 2020-04-17 21:27:16
问题 I have this code that parses and processes normal "f-string" template strings (See the usage part below for an example): from string import Formatter import sys _conversions = {'a': ascii, 'r': repr, 's': str} def z(template, locals_=None): if locals_ is None: previous_frame = sys._getframe(1) previous_frame_locals = previous_frame.f_locals locals_ = previous_frame_locals # locals_ = globals() result = [] parts = Formatter().parse(template) for part in parts: literal_text, field_name, format

Is there a formatted byte string literal in Python 3.6+?

北城以北 提交于 2020-04-10 06:47:10
问题 I'm looking for a formatted byte string literal. Specifically, something equivalent to name = "Hello" bytes(f"Some format string {name}") Possibly something like fb"Some format string {name}" . Does such a thing exist? 回答1: No. The idea is explicitly dismissed in the PEP: For the same reason that we don't support bytes.format() , you may not combine 'f' with 'b' string literals. The primary problem is that an object's __format__() method may return Unicode data that is not compatible with a

Build f-strings from normal strings

烈酒焚心 提交于 2020-01-04 06:36:06
问题 I'm trying the new f-strings, and I'm wondering if it's possible to "compile" a normal string into an f-string. So to have control on the evaluation time of the f-string, and be capable of defining f-strings before consuming them. Example pseudo-code: a = 'normal string with some curly {inside}' inside = 'in it!' print(a.make_f_string()) >>> 'normal string with some curly in it!' So basically my need is to define the f-string before the variable that it contains. or make a string an f-string.