对齐字母列?

时间:2014-10-19 17:50:08

标签: python alignment

这只是我代码的一部分,但我在调整它时遇到了麻烦 我希望它以下列方式打印输出:

N a m e
1 2 3 4

现在它做了类似的事情:

['J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h']
['[', '1', ',', ' ', '6', ',', ' ', '8', ',', ' ', '5', ',', ' ', '1', ',', ' ', '4', ',', ' ', '9', ',', ' ', '2', ',', ' ', '8', ']']

只是一个抬头我仍然是蟒蛇的新手哈哈

print("Your name and numeric value is:")
fullname = first_name + " " + last_name
print(list(fullname))

name = first_name + last_name
name = str.lower(name)
output = []
for letter in name:
    number = (ord(letter) - 97)%9 + 1
    output.append(number)
print(list(str(output)))

3 个答案:

答案 0 :(得分:1)

使用.join字符串方法:

  

name = first_name + last_name

     

name = str.lower(name)

     

输出=' ' .join(姓名)

' '在方法标记将加入零件之前。在这种情况下,空格。

如果名称['j','h','o','n']将返回j h o n

答案 1 :(得分:0)

这个怎么样:

first_name = 'Foo'
last_name = 'Bar'

print("Your name and numeric value is:")
fullname = first_name + ' ' + last_name
print(' '.join(fullname))

fullname = str.lower(fullname)
output = ''
for letter in fullname:
    if letter == ' ':
        output += '  '
    else:
        number = (ord(letter) - 97)%9 + 1
        output += str(number) + ' '
print(output)

输出:

F o o   B a r
6 6 6   2 1 9 

答案 2 :(得分:0)

而不是:

print(list(fullname))

使用:

print(''.join('%3s' % c for c in fullname))

而不是:

print(list(str(output)))

使用:

print(''.join('%3s' % n for n in output))

通过这种方式,即使数字超过一位数,您也可以获得2个空格并且右对齐的所有内容。

相关问题