用星号替换字符串中的所有字符 [英] replace all characters in a string with asterisks
问题描述
我有一个字符串
name = "Ben"
我变成了一个列表
word = list(name)
我想用星号替换列表中的字符.我该怎么做?
我尝试使用 .replace
函数,但这太具体了,并没有一次更改所有字符.
我需要一个适用于任何字符串的通用解决方案.
我想用星号替换列表中的字符
相反,创建一个只有星号的新字符串对象,就像这样
word = '*' * len(name)
在 Python 中,您可以将一个字符串与一个数字相乘以获得连接在一起的相同字符串.例如,
<预><代码>>>>'*' * 3'***'>>>'ABC' * 3'abcabcabc'I have a string
name = "Ben"
that I turn into a list
word = list(name)
I want to replace the characters of the list with asterisks. How can I do this?
I tried using the .replace
function, but that was too specific and didn't change all the characters at once.
I need a general solution that will work for any string.
I want to replace the characters of the list w/ asterisks
Instead, create a new string object with only asterisks, like this
word = '*' * len(name)
In Python, you can multiply a string with a number to get the same string concatenated. For example,
>>> '*' * 3
'***'
>>> 'abc' * 3
'abcabcabc'
这篇关于用星号替换字符串中的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!