[英]What does the * in *len() do?
在下面的代碼中,為什么len()前面有一個星號?
def censor (text,word):
if word in text:
text= text.replace(word, "*" *len(word))
return text
您為什么不能只輸入len(word)?
您的代碼有點誤導,應顯示為:
text = text.replace(word, "*" * len(word))
星號是乘法運算符-將字符串"*"
與要檢查的單詞的長度相乘以構建被檢查的字符串。
>>> word = "hello"
>>> len(word)
5
>>> "*" * len(word)
'*****'
您為什么不能只輸入len(word)?
len(word)
讓您5
。
然后我們將'*'
乘以5
,得到'*****'
代碼的作用是將星號乘以單詞的長度。 *
是乘法運算符。 您不能放入len
因為這樣代碼會出錯( "*"len(word)
是無效的語法)。 例如,如果有五個字符,它將放置五個星號。
len("qwert") * "*" = "*****"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.