Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中用于对齐字符串(向左、居中或向右)的函数_Python_String_Function_Formatting_Justify - Fatal编程技术网

python中用于对齐字符串(向左、居中或向右)的函数

python中用于对齐字符串(向左、居中或向右)的函数,python,string,function,formatting,justify,Python,String,Function,Formatting,Justify,我想在代码中使用一个函数来证明字符串的正确性。我卡住了,请看我的密码。 提前谢谢 def justify(s, pos): #(<string>, <[l]/[c]/[r]>) if len(s)<=70: if pos == l: print 30*' ' + s elif pos == c: print ((70 - len(s))/2)*' ' + s elif pos == r:

我想在代码中使用一个函数来证明字符串的正确性。我卡住了,请看我的密码。 提前谢谢

def justify(s, pos):      #(<string>, <[l]/[c]/[r]>)
if len(s)<=70:

    if pos == l:
        print 30*' ' + s
    elif pos == c:
        print ((70 - len(s))/2)*' ' + s
    elif pos == r:
        print (40 - len(s)*' ' + s

    else:
        print('You entered invalid argument-(use either r, c or l)')
else:
    print("The entered string is more than 70 character long. Couldn't be justified.")

你错过了第二次elif的一个括号。更正代码如下-

def justify(s, pos):
    if len(s)<=70:
        if pos == l:
            print 30*' ' + s
        elif pos == c:
            print ((70 - len(s))/2)*' ' + s
        elif pos == r:
            #you missed it here...
            print (40 - len(s))*' ' + s
        else:
            print('You entered invalid argument-(use either r, c or l)')
    else:
        print("The entered string is more than 70 character long. Couldn't be justified.")

看,它实际上做什么?错误我自己会带上%-70和%70的sprintf风格的面具,它们更干净。中心对齐可能需要一些额外的操作work@BrenBarn谢谢:
def justify2(s, pos):
    di = {"l" : "%-70s", "r" : "%70s"}

    if pos in ("l","r"):
        print ":" + di[pos] % s + ":"
    elif pos == "c":
        split = len(s) / 2
        s1, s2 = s[:split], s[split:]
        print ":" + "%35s" % s1 + "%-35s" % s2 + ":"
    else:
        "bad position:%s:" % (pos)


justify2("abc", "l")
justify2("def", "r")
justify2("xyz", "c")

:abc                                                                   :
:                                                                   def:
:                                  xyz                                 :