f-string

How to use gettext with python >3.6 f-strings

本秂侑毒 提交于 2019-12-10 01:16:16
问题 Previously you would use gettext as following: _('Hey {},').format(username) but what about new Python's f-string? f'Hey {username}' 回答1: '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. 回答2: Preface I know this question is quite old and already has a very legitimate answer but I’m going to be bold and give my answer to this as I

Using f-string with format depending on a condition

时光怂恿深爱的人放手 提交于 2019-12-08 16:03:14
问题 How can I use f-string with logic to format an int as a float ? I would like if ppl is True to format num to 2 decimal places, and if ppl is False to rformat it as whatever it is. Something like string = f'i am {num:.2f if ppl else num}' but this does not work. The below code demonstrates the behaviour that I want to achieve with a simpler f-string if possible: ppl = True num = 3 string = f'I am {num:.2f}' if ppl else f'I am {num}' print(string) #if ppl False #=> i am 3 #if ppl True #=> i am

Why isn't it possible to use “await” in f-strings?

旧城冷巷雨未停 提交于 2019-12-08 14:54:20
问题 Why isn't it possible to use "await" in f-strings? Is there any way to coerce f-strings into evaluating the format expressions in the context of a coroutine function? $ python3 Python 3.6.0 (default, Mar 4 2017, 12:32:37) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> async def a(): return 1 ... >>> async def b(): return 'The return value of await a() is {}.'.format(await a()) ... >>> async def c(

What are formatted string literals in Python 3.6?

╄→гoц情女王★ 提交于 2019-12-06 17:12:40
问题 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 which situations should I use this feature? Isn't explicit better than implicit? 回答1: Simple is better than complex. So here we have formatted string. It gives the simplicity to the string formatting, while keeping the code explicit (comprared to

Can you overload the Python 3.6 f-string's “operator”?

放肆的年华 提交于 2019-12-05 01:35:25
In Python 3.6, you can use f-strings like: >>> date = datetime.date(1991, 10, 12) >>> f'{date} was on a {date:%A}' '1991-10-12 was on a Saturday' I want to overload the method receiving the '%A' above. Can it be done? For example, if I wanted to write a dumb wrapper around datetime , I might expect this overloading to look something like: class MyDatetime: def __init__(self, my_datetime, some_other_value): self.dt = my_datetime self.some_other_value = some_other_value def __fstr__(self, format_str): return ( self.dt.strftime(format_str) + 'some other string' + str(self.some_other_value ) Yes ,

What are formatted string literals in Python 3.6?

China☆狼群 提交于 2019-12-04 22:49:20
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 which situations should I use this feature? Isn't explicit better than implicit? xmcp Simple is better than complex. So here we have formatted string. It gives the simplicity to the string formatting, while keeping the code explicit (comprared to other string formatting mechanisms). title = 'Mr.' name = 'Tom' count = 3 # This is explicit but complex

How to use gettext with python >3.6 f-strings

只谈情不闲聊 提交于 2019-12-04 22:43:51
Previously you would use gettext as following: _('Hey {},').format(username) but what about new Python's f-string? f'Hey {username}' '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. Preface I know this question is quite old and already has a very legitimate answer but I’m going to be bold and give my answer to this as I landed here while searching, and I am a fan of using f-strings myself and don’t want to lose out on its use but also

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

╄→尐↘猪︶ㄣ 提交于 2019-12-03 18:46:29
问题 I've downloaded a Python 3.6 alpha build from the Python Github repository, and one of my favourite new features is literal string formatting. It can be used like so: >>> x = 2 >>> f"x is {x}" "x is 2" This appears to do the same thing as using the format function on a str instance. However, one thing that I've noticed is that this literal string formatting is actually very slow compared to just calling format . Here's what timeit says about each method: >>> x = 2 >>> timeit.timeit(lambda: f

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

我们两清 提交于 2019-12-03 10:29:48
问题 The new Python 3.6 f-strings seem like a huge jump in string usability to me, and I would love to jump in and adopt them whole heartedly on new projects which might be running on older interpreters. 2.7, 3.3-3.5 support would be great but at the very least I would like to use these in Python 3.5 code bases. How can I import 3.6's formatted string literals for use by older interpreters? I understand that formatted string literals like f"Foo is {age} {units} old" are not breaking changes, so

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

僤鯓⒐⒋嵵緔 提交于 2019-12-03 00:59:04
The new Python 3.6 f-strings seem like a huge jump in string usability to me, and I would love to jump in and adopt them whole heartedly on new projects which might be running on older interpreters. 2.7, 3.3-3.5 support would be great but at the very least I would like to use these in Python 3.5 code bases. How can I import 3.6's formatted string literals for use by older interpreters? I understand that formatted string literals like f"Foo is {age} {units} old" are not breaking changes, so would not be included in a from __future__ import ... call. But the change is not back-ported (AFAIK) I