tap*_*per 472 python string string-formatting pad
我想用空格填写一个字符串.我知道以下适用于零:
>>> print "'%06d'"%4
'000004'
Run Code Online (Sandbox Code Playgroud)
但是当我想要这个时我该怎么办?
'hi '
Run Code Online (Sandbox Code Playgroud)
当然我可以测量弦长并做str+" "*leftover,但我想要最短路.
Fel*_*ing 654
你可以这样做str.ljust(width[, fillchar]):
返回长度的字符串左对齐字符串宽度.使用指定的fillchar完成填充(默认为空格).如果width小于,则返回原始字符串
len(s).
>>> 'hi'.ljust(10)
'hi '
Run Code Online (Sandbox Code Playgroud)
sim*_*mon 342
除非您需要支持Python 2.5,否则您可以(也可能应该)使用字符串格式化迷你语言,如下所示:
'{0: <16}'.format('Hi')
Run Code Online (Sandbox Code Playgroud)
Civ*_*Fan 112
新的(ish)字符串格式方法允许您使用嵌套关键字参数做一些有趣的事情.最简单的情况:
>>> '{message: <16}'.format(message='Hi')
'Hi '
Run Code Online (Sandbox Code Playgroud)
如果要16作为变量传入:
>>> '{message: <{width}}'.format(message='Hi', width=16)
'Hi '
Run Code Online (Sandbox Code Playgroud)
如果你想传递整个工具包和kaboodle的变量:
'{message:{fill}{align}{width}}'.format(
message='Hi',
fill=' ',
align='<',
width=16,
)
Run Code Online (Sandbox Code Playgroud)
结果(你猜对了):
'Hi '
Run Code Online (Sandbox Code Playgroud)
abb*_*bot 74
你可以试试这个:
print "'%-100s'" % 'hi'
Run Code Online (Sandbox Code Playgroud)
Gra*_*rus 55
正确的方法是使用官方文档中描述的Python格式语法
对于这种情况,它只是:
'{:10}'.format('hi')
哪些输出:
'hi '
说明:
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Run Code Online (Sandbox Code Playgroud)
几乎所有你需要知道的是^.
更新:从python 3.6开始,使用文字字符串插值更方便!
foo = 'foobar'
print(f'{foo:10} is great!')
# foobar is great!
Run Code Online (Sandbox Code Playgroud)
K.M*_*Mat 43
text = 'hi'
print(f'{text:10}') # 'hi '
Run Code Online (Sandbox Code Playgroud)
因为Python3.6您可以使用 f-strings文字插值。
可变空间:
value = 4
space = 10
# move value to left
print(f'foo {value:<{space}} bar') # foo 4 bar
# move value to right
print(f'foo {value:>{space}} bar') # foo 4 bar
# center value
print(f'foo {value:^{space}} bar') # foo 4 bar
Run Code Online (Sandbox Code Playgroud)
恒定空间:
value = 4
# move value to left
print(f'foo {value:<10} bar') # foo 4 bar
# move value to right
print(f'foo {value:>10} bar') # foo 4 bar
# center value
print(f'foo {value:^10} bar') # foo 4 bar
Run Code Online (Sandbox Code Playgroud)
如果您想用其他字符填充然后空格,请在开头指定:
value = 4
space = 10
padd = '_'
print(f'foo {value:{padd}^{space}} bar') # foo ____4_____ bar
print(f'foo {value:_^10} bar') # foo ____4_____ bar
Run Code Online (Sandbox Code Playgroud)
Joh*_*web 36
用途str.ljust():
>>> 'Hi'.ljust(6)
'Hi '
Run Code Online (Sandbox Code Playgroud)
您还应该考虑string.zfill(),str.ljust()以及str.center()字符串格式.这些可以链接并指定' 填充 '字符,因此:
>>> ('3'.zfill(8) + 'blind'.rjust(8) + 'mice'.ljust(8, '.')).center(40)
' 00000003 blindmice.... '
Run Code Online (Sandbox Code Playgroud)
这些字符串格式化操作具有在Python v2和v3中工作的优点.
看看pydoc str有时候:那里有很多好东西.
WAF*_*WAF 25
从Python 3.6开始,你就可以做到
>>> strng = 'hi'
>>> f'{strng: <10}'
Run Code Online (Sandbox Code Playgroud)
使用文字字符串插值.
或者,如果你的填充大小是一个变量,就像这样(感谢@Matt M.!):
>>> to_pad = 10
>>> f'{strng: <{to_pad}}'
Run Code Online (Sandbox Code Playgroud)
使用Python 2.7的字符串迷你格式:
'{0: <8}'.format('123')
Run Code Online (Sandbox Code Playgroud)
左对齐,并使用''字符填充8个字符.
使用切片不是更Pythonic吗?
例如,要在字符串右侧填充空格,直到长度达到 10 个字符:
>>> x = "string"
>>> (x + " " * 10)[:10]
'string '
Run Code Online (Sandbox Code Playgroud)
要在左侧填充空格,直到长度达到 15 个字符:
>>> (" " * 15 + x)[-15:]
' string'
Run Code Online (Sandbox Code Playgroud)
当然,它需要知道您想要填充多长,但不需要测量您开始使用的字符串的长度。
| 归档时间: |
|
| 查看次数: |
454942 次 |
| 最近记录: |