Python 如何用字符替换字符串的一部分
我试图创建一个打印到'n'的代码,并用“*”替换其余字符。 这是我到目前为止的代码” 但它的输出Python 如何用字符替换字符串的一部分,python,Python,我试图创建一个打印到'n'的代码,并用“*”替换其余字符。 这是我到目前为止的代码” 但它的输出 replace("hello", 2) 'he*' 它应该是‘he***’您应该用要替换的字符数乘以“*”。此外,您应该在s[n://code>之后添加(len(s)-n)*“*”,而不是替换(因为同一组字符可能会出现在字符串中的多个位置)。您可以按如下操作: def replace(s,n): return s[:n]+(len(s)-n)*"*" replace('hel
replace("hello", 2)
'he*'
它应该是‘he***’您应该用要替换的字符数乘以“*”
。此外,您应该在s[n://code>之后添加(len(s)-n)*“*”
,而不是替换(因为同一组字符可能会出现在字符串中的多个位置)。您可以按如下操作:
def replace(s,n):
return s[:n]+(len(s)-n)*"*"
replace('hello', 2)
这将打印出'he***'
您应该将“*”
乘以您要替换的字符数。此外,您应该在s[n:][/code>之后添加(len(s)-n)*“*”
,而不是替换(因为同一组字符可能会出现在字符串中的多个位置)。您可以按如下操作:
def replace(s,n):
return s[:n]+(len(s)-n)*"*"
replace('hello', 2)
此打印“代码>”/代码>
有两个基本问题:第一,<代码> S.Exchange < /C>将用第二个参数替换整个第一个参数,甚至更重要的是,它在字符串上找到它的任何位置替换它。因此,请考虑下面的例子:
>>> def replace(s,n):
... return s.replace(s[n:], "*")
...
>>> replace('lalahahalala', 8)
'*haha*'
>>>
相反,您应该采取另一种方法,迭代字符串,如果索引是
,则返回该字符串中的字符,否则返回'*'
:
>>> def replace(s, n):
... return ''.join(c if i < n else '*' for i,c in enumerate(s))
...
>>> replace("hello", 2)
'he***'
>>> replace('lalahahalala', 8)
'lalahaha****'
def更换(s,n):
…返回“”。加入(如果枚举中的i,c的i>>替换(“你好”,2)
“他***”
>>>替换('lalahalala',8)
“拉拉哈哈****”
以下是使用for循环而不是生成器表达式的上述版本:
>>> def replace(s, n):
... char_list = []
... for i, c in enumerate(s):
... if i < n:
... char_list.append(c)
... else:
... char_list.append('*')
... return ''.join(char_list)
...
>>> replace('hello', 2)
'he***'
>>> replace('lalahahalala', 8)
'lalahaha****'
>>>
def更换(s,n):
…字符列表=[]
…对于枚举中的i,c:
…如果i>>替换('hello',2)
“他***”
>>>替换('lalahalala',8)
“拉拉哈哈****”
>>>
有两个基本问题:第一,<代码> S.EXP/<代码>将用第二个参数替换整个第一个参数,甚至更重要的是,它在字符串上找到它的任何位置替换它。因此,请考虑下面的例子:
>>> def replace(s,n):
... return s.replace(s[n:], "*")
...
>>> replace('lalahahalala', 8)
'*haha*'
>>>
相反,您应该采取另一种方法,迭代字符串,如果索引是
,则返回该字符串中的字符,否则返回'*'
:
>>> def replace(s, n):
... return ''.join(c if i < n else '*' for i,c in enumerate(s))
...
>>> replace("hello", 2)
'he***'
>>> replace('lalahahalala', 8)
'lalahaha****'
def更换(s,n):
…返回“”。加入(如果枚举中的i,c的i>>替换(“你好”,2)
“他***”
>>>替换('lalahalala',8)
“拉拉哈哈****”
以下是使用for循环而不是生成器表达式的上述版本:
>>> def replace(s, n):
... char_list = []
... for i, c in enumerate(s):
... if i < n:
... char_list.append(c)
... else:
... char_list.append('*')
... return ''.join(char_list)
...
>>> replace('hello', 2)
'he***'
>>> replace('lalahahalala', 8)
'lalahaha****'
>>>
def更换(s,n):
…字符列表=[]
…对于枚举中的i,c:
…如果i替换方法从根本上说是有缺陷的,因为替换方法在源字符串中的任何位置寻找一个子字符串来替换,而不是替换某个位置的字符。即使使用@MiriamFarber的“固定”版本(现在他编辑了它,查看修订历史),您也会得到错误的输出,如
replace("chachacha", 6) # returns *********
您需要的是在请求的位置截断字符串,并向其附加一个与删除的字符数量相同的星号字符串
def replace(s, n):
return s[:n] + '*'*(len(s)-n)
replace
方法从根本上是有缺陷的,因为replace
在源字符串中的任何位置查找要替换的子字符串,而不是替换某些位置的字符。即使使用@MiriamFarber的“固定”版本(现在他编辑了它,查看修订历史),您也会得到错误的输出,如
replace("chachacha", 6) # returns *********
您需要的是在请求的位置截断字符串,并向其附加一个与删除的字符数量相同的星号字符串
def replace(s, n):
return s[:n] + '*'*(len(s)-n)
您可以对
s
进行切片,直到n
位置,然后使用*
运算符连接字符串的其余部分。您不需要使用replace
方法:
def replace(s,n):
return s[:n] + (len(s)-n)*'*'
输出为:
replace('hello', 2)
'he***'
您可以对
s
进行切片,直到n
位置,然后使用*
运算符连接字符串的其余部分。您不需要使用replace
方法:
def replace(s,n):
return s[:n] + (len(s)-n)*'*'
输出为:
replace('hello', 2)
'he***'
您是否尝试计算它将替换多少个字符?您是否尝试计算它将替换多少个字符?