且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用星号替换字符串中的所有字符

更新时间:2023-02-12 19:19:29

我想用星号替换列表中的字符

相反,创建一个只有星号的新字符串对象,就像这样

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'