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.
我确信这很多地方都有,但我不知道我正在尝试的动作的确切名称,所以我无法真正查找它。我一直在阅读官方的Python书30分钟,试图找出如何做到这一点。
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.
我知道它可能是某种形式的格式化,但我找不到确切的方法来做到这一点。帮助将不胜感激。
6 个解决方案
#1
57
This is super simple with format
:
格式非常简单:
>>> a = "John"
>>> "{:<15}".format(a)
'John '
#2
43
You can use the ljust
method on strings.
您可以在字符串上使用ljust方法。
>>> name = 'John'
>>> name.ljust(15)
'John '
Note that if the name is longer than 15 characters, ljust
won't truncate it. If you want to end up with exactly 15 characters, you can slice the resulting string:
请注意,如果名称超过15个字符,则不会截断它。如果您希望最终得到15个字符,则可以对结果字符串进行切片:
>>> name.ljust(15)[:15]
#3
1
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
这将添加所需的空格数,前提是该字段的长度> =所提供名称的长度
#4
1
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 += " "
#5
0
name = "John" // your variable
result = (name+" ")[:15] # this adds 15 spaces to the "name"
# but cuts it at 15 characters
#6
0
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
#1
57
This is super simple with format
:
格式非常简单:
>>> a = "John"
>>> "{:<15}".format(a)
'John '
#2
43
You can use the ljust
method on strings.
您可以在字符串上使用ljust方法。
>>> name = 'John'
>>> name.ljust(15)
'John '
Note that if the name is longer than 15 characters, ljust
won't truncate it. If you want to end up with exactly 15 characters, you can slice the resulting string:
请注意,如果名称超过15个字符,则不会截断它。如果您希望最终得到15个字符,则可以对结果字符串进行切片:
>>> name.ljust(15)[:15]
#3
1
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
这将添加所需的空格数,前提是该字段的长度> =所提供名称的长度
#4
1
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 += " "
#5
0
name = "John" // your variable
result = (name+" ")[:15] # this adds 15 spaces to the "name"
# but cuts it at 15 characters
#6
0
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