Skip to main content
 首页 » 编程设计

python之用 '#' 改变字符串的一部分

2025年05月04日32wayfarer

想看看我的方向是否正确。我必须将字符串的最后四个字符以外的所有字符都更改为#。到目前为止,我有两个想法。

第一个:

def maskify(cc): 
   cc = raw_input("Enter passcode: ") 
   n = len(cc) 
   cc.replace(cc[0:n-4], #)  # this one gives me the unexpected EOF while parsing error 

第二个(我认为这个更接近,因为它应该需要一种算法):

def maskify(cc): 
   cc = raw_input("Enter passcode: ")   
   n = len(cc) 
   for i in range (0, n-4):  # i think a for loop would be good but i don't know how i'm going to use it yet 
      cc.replace(                  #not entirely sure what to put here 
   pass                            

请您参考如下方法:

cc = raw_input("Enter passcode: ") 
cc = ''.join(('#' * (len(cc) - 4), cc[-4:]))