如何在print语句中添加字符?

时间:2015-04-19 00:14:34

标签: python python-3.x printing format

我希望以下函数的输出有一个" |"开头的字符和另一个" |"最后没有添加空格。如何将这两个字符添加到最终的打印语句中?

#!/usr/bin/python3


zipCode = raw_input("Enter a zipcode: ")

def printBarCode(zipCode):
    a = 0
    total = sum(map(int,str(zipCode)))
    if total <= 10:
            a= 10-total
    elif total <= 20:
            a = 20-total
    elif total <= 30:
            a = 30-total
    elif total <= 40:
            a = 40-total
    elif total <= 50:
            a = 50-total
    checkDigit= str(a)

    codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

    List = []

    for letter in zipCode:
            List.append(codes[int(letter)])

    for i in checkDigit:
            List.append(codes[int(i)])
    print ''.join(List)

printBarCode(zipCode)

1 个答案:

答案 0 :(得分:2)

使用str.format()

print "|{}|".format(''.join(List))
相关问题