繁体   English   中英

如何在打印语句中添加字符?

[英]How do I add characters in print statement?

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

#!/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)

使用str.format()

print "|{}|".format(''.join(List))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM