如何在Python中用空格将字符串填充到固定长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20309255/
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 20:09:52  来源:igfitidea点击:

How to pad a string to a fixed length with spaces in Python?

pythonstringformat

提问by user2977230

I'm sure this is covered in plenty of places, but I don't know the exact name of the action I'm trying to do so I can't really look it up. I've been reading an official Python book for 30 minutes trying to find out how to do this.

我确信这在很多地方都有涉及,但我不知道我正在尝试执行的操作的确切名称,因此我无法真正查找。我已经阅读了 30 分钟的 Python 官方书籍,试图找出如何做到这一点。

Problem: I need to put a string in a certain length "field".

问题:我需要在一定长度的“字段”中放入一个字符串。

For example, if the name field was 15 characters long, and my name was John, I would get "John" followed by 11 spaces to create the 15 character field.

例如,如果姓名字段的长度为 15 个字符,而我的名字是 John,我将得到“John”后跟 11 个空格以创建 15 个字符的字段。

I need this to work for any string put in for the variable "name".

我需要这个来处理为变量“name”输入的任何字符串。

I know it will likely be some form of formatting, but I can't find the exact way to do this. Help would be appreciated.

我知道这可能是某种形式的格式,但我找不到执行此操作的确切方法。帮助将不胜感激。

采纳答案by Games Brainiac

This is super simple with format:

这非常简单format

>>> a = "John"
>>> "{:<15}".format(a)
'John           '

回答by Aswin Murugesh

string = ""
name = raw_input() #The value at the field
length = input() #the length of the field
string += name
string += " "*(length-len(name)) # Add extra spaces

This will add the number of spaces needed, provided the field has length >= the length of the name provided

这将添加所需的空格数,前提是该字段的长度 >= 所提供名称的长度

回答by Matt

First check to see if the string's length needs to be shortened, then add spaces until it is as long as the field length.

首先检查字符串的长度是否需要缩短,然后添加空格直到它与字段长度一样长。

fieldLength = 15
string1 = string1[0:15] # If it needs to be shortened, shorten it
while len(string1) < fieldLength:
    rand += " "

回答by Matt

name = "John" // your variable
result = (name+"               ")[:15] # this adds 15 spaces to the "name"
                                       # but cuts it at 15 characters

回答by Ismail Badawi

You can use the ljustmethod on strings.

您可以ljust在 strings 上使用该方法

>>> name = 'John'
>>> name.ljust(15)
'John           '

Note that if the name is longer than 15 characters, ljustwon't truncate it. If you want to end up with exactly 15 characters, you can slice the resulting string:

请注意,如果名称超过 15 个字符,ljust则不会截断它。如果您想以 15 个字符结束,您可以对结果字符串进行切片:

>>> name.ljust(15)[:15]

回答by dragon40226

Just whipped this up for my problem, it just adds a space until the length of string is more than the min_length you give it.

只是针对我的问题提出了这个问题,它只是添加了一个空格,直到字符串的长度大于您给它的 min_length 为止。

def format_string(str, min_length):
    while len(str) < min_length:
        str += " "
    return str