2011-05-03 82 views
1

我在PySchools.com上運行了一些Python實踐並陷入了困境。我相信這是一個簡單的任務。任何人?PySchools - 主題1問題7

您可以使用len(x)函數來找出字符串中的字符數。

例子

>>> greetings = "Hello World" 
>>> len(greetings) # get the length of string 
11 
>>> greetings[0] # get the 1st character 
'H' 

的問題在這裏如下:

greeting = "Hello Google!" 
# number of characters stored in the variable greeting 
number_of_char = len(greeting) 

# repeat the greetings based on the number of character in 'greeting' 
greetings = 

http://www.pyschools.com/quiz/view_question/s1-q7

+0

那又是什麼問題? – 2011-05-03 08:27:03

+0

對不起,只是注意到了它。它在這裏說「重複根據'問候'中的字符數問候」 – LynxLee 2011-05-03 08:28:22

+0

我不確定'重複問候......'是什麼意思。這是他們想要的嗎? 'greetings = number_of_char * greeting'或者他們可能想要'問候= number_of_char * [問候]'?也許'''.join(number_of_char * [問候])'? – 2011-05-03 08:30:03

回答

2

您可以通過一個整數,以多次重複串乘以一個字符串。

>>> 'abc' * 2 
'abcabc' 
0
greetings = "Hello Google" 
repeated_greetings = len(greetings) * greetings 
print repeated_greetings 

> '你好GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello GoogleHello谷歌'

1

你可以簡單地乘以num_of_char來問候

做到這一點
greetings = number_of_char * greeting