Hướng dẫn python f-string space padding
I want to fill out a string with spaces. I know that the following works for zero's: Show
But what should I do when I want this?:
of course I can measure string length and do
codeforester 35.7k16 gold badges101 silver badges126 bronze badges asked Apr 15, 2011 at 12:22
1 You can do this with
answered Apr 15, 2011 at 12:24
Felix KlingFelix Kling 769k171 gold badges1068 silver badges1114 bronze badges 5 For a flexible method that works even when formatting complicated string, you probably should use the string-formatting mini-language, using either f-strings
or the
Neuron 4,5754 gold badges32 silver badges53 bronze badges answered Apr 15, 2011 at 12:41
simonsimon 14.3k4 gold badges43 silver badges67 bronze badges 9 The new(ish) string format method lets you do some fun stuff with nested keyword arguments. The simplest case:
If you want to pass in
If you want to pass in variables for the whole kit and kaboodle:
Which results in (you guessed it):
And for all these, you can use python 3.6+ f-strings:
And of course the result:
answered Aug 13, 2014 at 18:40
CivFanCivFan 12.3k9 gold badges39 silver badges57 bronze badges 2 You can try this:
answered Apr 15, 2011 at 12:24
abbotabbot 26.6k6 gold badges51 silver badges56 bronze badges 5 Correct way of doing this would be to use Python's format syntax as described in the official documentation For this case it would simply be: Explanation:
Pretty much all you need to know is there ^. Update: as of python 3.6 it's even more convenient with literal string interpolation!
Azat Ibrakov 8,9589 gold badges37 silver badges45 bronze badges answered Jul 21, 2016 at 13:33
GranitosaurusGranitosaurus 19.5k4 gold badges53 silver badges76 bronze badges Use
You should also consider
These string formatting operations have the advantage of working in Python v2 and v3. Take a look at answered Apr 15, 2011 at 12:27
johnsywebjohnsyweb 132k23 gold badges178 silver badges239 bronze badges 1 As of Python 3.6 you can just do
with literal string interpolation. Or, if your padding size is in a variable, like this (thanks @Matt M.!):
answered Jul 6, 2016 at 16:05
WAFWAF 9853 gold badges14 silver badges31 bronze badges 2 TL;DR
Longer explanationSince Variable space:
Constant space:
If you want to padd with some other char then space, specify it at the beginning:
answered Sep 17, 2021 at 8:55
K.MatK.Mat 1,1009 silver badges15 bronze badges 2 you can also center your string:
answered Jul 24, 2012 at 16:14
RemiRemi 19.9k8 gold badges55 silver badges41 bronze badges Use Python 2.7's mini formatting for strings:
This left aligns, and pads to 8 characters with the ' ' character. answered Apr 15, 2011 at 12:58
aodjaodj 2,0831 gold badge12 silver badges12 bronze badges 2 Just remove the 0 and it will add space instead:
eldarerathis 34.8k10 gold badges89 silver badges93 bronze badges answered Jun 12, 2015 at 13:30
Amir MofakharAmir Mofakhar 6,5572 gold badges13 silver badges5 bronze badges Wouldn't it be more pythonic to use slicing? For example, to pad a string with spaces on the right until it's 10 characters long:
To pad it with spaces on the left until it's 15 characters long:
It requires knowing how long you want to pad to, of course, but it doesn't require measuring the length of the string you're starting with. answered Sep 15, 2015 at 6:09
5 A nice trick to use in place of the various print formats: (1) Pad with spaces to the right:
(2) Pad with leading zeros on the left:
answered Jan 29, 2019 at 19:40
Erik AndersonErik Anderson 4,6153 gold badges29 silver badges28 bronze badges 2 You could do it using list comprehension, this'd give you an idea about the number of spaces too and would be a one liner.
answered Nov 23, 2018 at 13:34
2 |