从python 3.6开始,将值格式化为字符串的最短,最高效的方法是PEP 498 f字符串(例如f'Hello {name}')。
在现有代码库中将旧的格式设置类型('Hello %s' % name,'Hello {}'.format(name))转换为新的格式设置似乎是可以自动化的任务,但是我找不到执行此操作的任何工具。是否有工具可以将使用旧字符串格式的文字自动转换为f字符串?
我正在尝试在 Python3(.7) 中使用三引号字符串来构建一些格式化字符串。
我有一个内部字符串列表,所有这些都需要添加到:
This is some text
across multiple
lines.
Run Code Online (Sandbox Code Playgroud)
以及一个应包含内部字符串的字符串
data{
// string goes here
}
Run Code Online (Sandbox Code Playgroud)
创建内部字符串时我无法制表符。dedent所以,我的想法是与 Python3 三引号 fstring 一起使用:
import textwrap
inner_str = textwrap.dedent(
'''\
This is some text
across multiple
lines.'''
)
full_str = textwrap.dedent(
f'''\
data{{
// This should all be tabbed
{inner_str}
}}'''
)
print(full_str)
Run Code Online (Sandbox Code Playgroud)
但是,不会保留缩进:
data{
// This should all be tabbed
This is some text
across multiple
lines.
}
Run Code Online (Sandbox Code Playgroud)
期望的结果:
data{
// This should all be tabbed
This is …Run Code Online (Sandbox Code Playgroud) 在大部分时间使用 VSCode 之后,我将切换回使用 VS2017 进行大部分 Python 开发。在 VSCode 中,我能够很好地使用带有语法高亮和智能感知的 f 字符串。
在 VS2017 中,当我使用 f 字符串时,它只会显示为原始字符串而不突出显示。这在程序运行时有效,但对于调试和编写,它使事情变得更加痛苦。
有谁知道这是否可以添加到VS2017?两者都在运行 python 3.6,所以我认为不应该有问题。
为了澄清,我指的是这些:
f'Hello My Name Is { user.Name }'
Run Code Online (Sandbox Code Playgroud) 我是 PHP 新手,所以请原谅这个问题。
我想知道 PHP 是否有字符串格式函数,例如 Python 的 f-strings 函数,而不是 str.format()。我看过一些关于这个主题的帖子,但大多数被接受为答案的例子是指 Python 处理格式化字符串的旧方式str.format()。在我的情况下,我想使用格式化的字符串构建一个变量,例如(Python):
f_name = "John"
l_name = "Smith"
sample = f`{f_name}'s last name is {l_name}.`
print(sample)
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用(PHP):
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
Run Code Online (Sandbox Code Playgroud)
但是如果我想$format用作变量呢?主要思想是基于其他变量创建一个动态变量,例如:
$db_type = $settings['db_type']; # mysql
$db_host = $settings['db_host']; # localhost
$db_name = $settings['db_name']; # sample
var $format = "%s:host=%s; dbname=%s";
# Not sure what to do after that, …Run Code Online (Sandbox Code Playgroud) Python 3.8 允许使用自记录表达式和调试=,例如:
print(f'{myvar=}')
Run Code Online (Sandbox Code Playgroud)
是否可以在新行上打印输出?这对于具有多行输出的变量(例如数据框)非常有用,例如
import pandas as pd
df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion', 'monkey']})
print(f'{df=}')
Run Code Online (Sandbox Code Playgroud)
df =
animal
0 alligator
1 bee
2 falcon
3 lion
4 monkey
Run Code Online (Sandbox Code Playgroud) 当我以类似的方式使用带有格式说明符的 f 字符串时
>>> x = 1
>>> f'{ x:02 }'
Run Code Online (Sandbox Code Playgroud)
它给了我ValueError: Unknown format code '\x20' for object of type 'int'。为了解决这个问题,我必须从 f 字符串表达式中删除周围的空格,例如f'{x:02}'.
为什么会这样呢?
这两个print语句产生相同的结果,至少对我来说,第一个版本看起来更具可读性。
我是否应该坚持使用该f'版本,因为它会在以后给我带来问题,或者导致性能更差,或者不遵循当前的 Python 标准?或者这只是这些版本之一的一致使用问题?
print('My first bicycle was a ' + bicycles[1])
print(f'My first bicycle was a {bicycles[1]})')
Run Code Online (Sandbox Code Playgroud) 对于以下行:
print("{0: <24}".format("==> core=") + str(my_dict["core"]))
Run Code Online (Sandbox Code Playgroud)
我收到以下警告消息:
[考虑使用 f 字符串] 格式化可能是 f 字符串的常规字符串 [C0209]
我可以使用重新格式化它吗f-string?
我有以下 f 字符串:
f"Something{function(parameter)}"
Run Code Online (Sandbox Code Playgroud)
我想对该参数进行硬编码,它是一个字符串:
f"Something{function("foobar")}"
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
SyntaxError: f-string: unmatched '('
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
为什么f'{'one'}'语法无效?在我看来,如果 f 字符串是明智的,那么一旦 a{出现,其中的所有内容都{ ... }应该被视为 Python 代码。
语言设计者决定将字符串的结束引号视为字符串结束的原因是什么,即使是在 的内部{ ... }?
由此带来什么好处?
关闭后编辑
注意:这是一个老问题。从 python 3.12 开始,这实际上是有效的语法。请参阅:https ://docs.python.org/3/whatsnew/3.12.html#whatsnew312-pep701
f-string ×10
python ×9
python-3.x ×4
string ×3
formatting ×1
ide ×1
indentation ×1
php ×1
pylint ×1