相关疑难解决方法(0)

如何用空格填写Python字符串?

我想用空格填写一个字符串.我知道以下适用于零:

>>> print  "'%06d'"%4
'000004'
Run Code Online (Sandbox Code Playgroud)

但是当我想要这个时我该怎么办?

'hi    '
Run Code Online (Sandbox Code Playgroud)

当然我可以测量弦长并做str+" "*leftover,但我想要最短路.

python string string-formatting pad

472
推荐指数
12
解决办法
45万
查看次数

使用 f 字符串格式化字符串宽度

我正在尝试创建给定宽度的标题并想使用

>>> print(f'{"string":15<}|')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: Unknown format code '<' for object of type 'str'
Run Code Online (Sandbox Code Playgroud)

我们是否打算像这样拼凑标题,还是我遗漏了有关 f 字符串的内容?

print('string'+" "*(15-len('string'))+"|")
string         |
Run Code Online (Sandbox Code Playgroud)

python f-string

2
推荐指数
1
解决办法
3075
查看次数

标签 统计

python ×2

f-string ×1

pad ×1

string ×1

string-formatting ×1