python如果输入10个数字,如果列表中的数字小于十,则在列表中的数字前放置一个0(在python中)...

Write a Python program that will ask the user to enter a string of lower-case characters and then print its corresponding two-digit code. For example, if the input is "home", the output should be "08151305".

Currently I have my code working to make a list of all the number, but I cannot

get it to add a 0 in front of the single digit numbers.

def word ():

output = []

input = raw_input("please enter a string of lowercase characters: ")

for character in input:

number = ord(character) - 96

output.append(number)

print output

This is the output I get:

word()

please enter a string of lowercase characters: abcdefghijklmnopqrstuvwxyz

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]

I think I may need to change the list to a string or to integers to do this but

I am not sure how to do that.

解决方案

output.append("%02d" % number) should do it. This uses Python string formatting operations to do left zero padding.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值