如何在 Python 中填充 n 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4008546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:49:42  来源:igfitidea点击:

How to pad with n characters in Python

pythonstring

提问by Gusto

I should de?ne a function pad_with_n_chars(s, n, c)that takes a string 's', an integer 'n', and a character 'c' and returns a string consisting of 's' padded with 'c' to create a string with a centered 's' of length 'n'. For example, pad_with_n_chars(”dog”, 5, ”x”)should return the string "xdogx".

我应该定义一个函数pad_with_n_chars(s, n, c),它接受一个字符串 's'、一个整数 'n' 和一个字符 'c' 并返回一个由 's' 填充的字符串组成的字符串 'c' 以创建一个以 's 为中心的字符串' 长度为 'n'。例如, pad_with_n_chars(”dog”, 5, ”x”)应该返回字符串“ xdogx”。

采纳答案by unutbu

With Python2.6 or better, there's no need to define your own function; the string formatmethod can do all this for you:

使用 Python2.6 或更高版本,无需定义自己的函数;字符串格式方法可以为您完成所有这些:

In [18]: '{s:{c}^{n}}'.format(s='dog',n=5,c='x')
Out[18]: 'xdogx'

回答by Georg Sch?lly

It looks like you're only looking for pointers, not a complete solution. So here's one:

看起来您只是在寻找指针,而不是完整的解决方案。所以这是一个:

You can multiply strings in python:

你可以在 python 中乘以字符串:

>>> "A" * 4
'AAAA'

Additionally I would use better names, instead of sI'd use text, which is much clearer. Even if your current professor (I suppose you're learning Python in university.) uses such horrible abbreviations.

此外,我会使用更好的名称,而不是s我会使用text,这更清晰。即使你现在的教授(我想你在大学里学习 Python。)使用如此可怕的缩写。

回答by ghostdog74

well, since this is a homework question, you probably won't understand what's going on if you use the "batteries" that are included.

好吧,由于这是一个家庭作业问题,如果您使用随附的“电池”,您可能不会明白发生了什么。

def pad_with_n_chars(s, n, c):
    r=n - len(s)
    if r%2==0:
       pad=r/2*c
       return pad + s + pad
    else:
       print "what to do if odd ? "
       #return 1
print pad_with_n_chars("doggy",9,"y")

Alternatively, when you are not schooling anymore.

或者,当您不再上学时。

>>> "dog".center(5,"x")
'xdogx'

回答by Kevin Postal

print '=' * 60
header = lambda x: '%s%s%s' % ('=' * (abs(int(len(x)) - 60) / 2 ),x,'=' * (abs(int(len(x)) - 60) / 2 ) )
print header("Bayors")

回答by Aditya Advani

yeah just use ljust or rjust to left-justify (pad right) and right-justify (pad left) with any given character.

是的,只需使用 ljust 或 rjust 对任何给定字符进行左对齐(右对齐)和右对齐(左对齐)。

For example ... to make '111' a 5 digit string padded with 'x'es

例如 ... 使 '111' 成为一个用 'x'es 填充的 5 位字符串

In Python3.6:

在 Python3.6 中:

>>> '111'.ljust(5, 'x')
111xx

>>> '111'.rjust(5, 'x')
xx111

回答by GooDeeJaY

In Python 3.x there are string methods: ljust, rjustand center.

在 Python 3.x 中有字符串方法:ljust,rjustcenter.

I created a function:

我创建了一个函数:

def header(txt: str, width=45, filler='-', align='c'):
    assert align in 'lcr'
    return {'l': txt.ljust, 'c': txt.center, 'r': txt.rjust}[align](width, filler)

print(header("Hello World"))
print(header("Hello World", align='l'))
print(header("Hello World", align='r'))

[Ouput]:

[输出]:

-----------------Hello World-----------------
Hello World----------------------------------
----------------------------------Hello World