简体   繁体   English

Python - 如何用左右空格填充字符串?

[英]Python - How can I pad a string with spaces from the right and left?

I have two scenarios where I need to pad a string with whitespaces up to a certain length, in both the left and right directions (in separate cases).我有两种情况,我需要在左右方向(在不同的情况下)用空格填充一个字符串到一定长度。 For instance, I have the string:例如,我有字符串:

TEST

but I need to make the string variable但我需要使字符串变量

_____TEST1

so that the actual string variable is 10 characters in length (led by 5 spaces in this case).以便实际字符串变量的长度为 10 个字符(在这种情况下由 5 个空格引导)。 NOTE : I am showing underscores to represent whitespace (the markdown doesn't look right on SO otherwise).注意:我显示下划线来表示空格(否则降价看起来不正确)。

I also need to figure out how to reverse it and pad whitespace from the other direction:我还需要弄清楚如何反转它并从另一个方向填充空白:

TEST2_____

Are there any string helper functions to do this?是否有任何字符串辅助函数可以做到这一点? Or would I need to create a character array to manage it?或者我需要创建一个字符数组来管理它吗?

Also note, that I am trying to keep the string length a variable (I used a length of 10 in the examples above, but I'll need to be able to change this).另请注意,我试图将字符串长度保持为一个变量(我在上面的示例中使用了 10 的长度,但我需要能够更改它)。

Any help would be awesome.任何帮助都是极好的。 If there are any python functions to manage this, I'd rather avoid having to write something from the ground up.如果有任何python函数来管理这个,我宁愿避免从头开始写一些东西。

Thanks!谢谢!

You can look intostr.ljust and str.rjust I believe.我相信你可以查看str.ljuststr.rjust

The alternative is probably to use the format method:另一种方法可能是使用format方法:

>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'

Python3 f string usage Python3 f 字符串用法

l = "left aligned"
print(f"{l.ljust(30)}")

r = "right aligned"
print(f"{r.rjust(30)}")

c = "center aligned"
print(f"{c.center(30)}")

>>> l = "left aligned"
>>> print(f"{l.ljust(30)}")
left aligned                  

>>> r = "right aligned"
>>> print(f"{r.rjust(30)}")
                 right aligned

>>> print(f"{c.center(30)}")
        center aligned        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中使用%s向右填充空格? - how to pad spaces to the right using %s in python? 如何在 Python 中用空格将字符串填充到固定长度? - How to pad a string to a fixed length with spaces in Python? 如何在 Python 中用零向右填充数字字符串? - How to pad a numeric string with zeros to the right in Python? 如何在Python中给定字符串的左侧和右侧生成一个没有任何前导空格的新字符串? - How to produce a new string withotu any leading spaces on the left and right side of the given string in Python? 为什么 Python 中的列表元素是从左到右索引的? 我怎样才能扭转这种局面? - Why are list elements in Python indexed from left to right? How can I reverse this? 如何使用 python 代码在 ubuntu 中使右/左扬声器静音? - How can I mute right/left speaker in ubuntu with python code? 如何修改它以从右到左读取并提取行? 而不是从左到右阅读 - How can I modify this to read from right to left and extract the lines? Instead of reading left to right 如何从左到右和从上到下对轮廓进行排序? - How can I sort contours from left to right and top to bottom? 如何添加来自左右的敌人? - How can I add enemies coming from left and right? 我如何从左到右绘制轮廓答题卡 - how can i drawContours answer sheet from left to right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM