print format
Dave Angel
davea at ieee.org
Mon Dec 14 22:28:11 EST 2009
mattia wrote:
> Hi all, I wanto to print just the first 5 characters of a string, why
> this doesn't work (py3.1)?
>
>>>> print("{0:5}".format("123456789"))
>>>>
> 123456789
> I know I could use print("123456789"[:5]), yeah it's a stupid example,
> but isn't format for string formatting?
>
> Thanks, Mattia
>
>
I tested following statement in 2.6, not 3.1, but I think it's the same.
The width field of the format is a *minimum* width. So you can use it
to force padding, but not truncation. If you need the string truncated,
just use slicing, as you already showed.
DaveA
More information about the Python-list
mailing list