m0_69776767 2022-06-15 20:18 采纳率: 60%
浏览 29
已结题

Python字符号码替换

img


输入:一个18个数字的身份证号码
输出:替换后的字符

俺写不出来ballball各位有人会吗

  • 写回答

2条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-06-16 07:16
    关注

    加入了号码初步判断,不要的去掉while那两行
    字串转列表替换比较简洁: idlst[6:14] = ['*']*8

    idstr = input('请输入身份证号:')
    
    while(len(idstr)!=18 or (idstr[-1]!='X' and not idstr.isnumeric()) or (idstr[-1]=='X' and not idstr[:-1].isnumeric())):
        idstr = input('身份证号不正确,请重输:')
    
    idlst = list(idstr)
    idlst[6:14] = ['*']*8
    
    print('替换之后为:',''.join(idlst))
    

    不转列表,直接用 字符串切片 相接也行:

    idstr = input('请输入身份证号:')
    
    while(len(idstr)!=18 or (idstr[-1]!='X' and not idstr.isnumeric()) or (idstr[-1]=='X' and not idstr[:-1].isnumeric())):
        idstr = input('身份证号不正确,请重输:')
    
    idstr = idstr[:6] + '*'*8 + idstr[-4:]
    
    print('替换之后为:',idstr)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 修改了问题 6月15日
  • 修改了问题 6月15日
  • 展开全部