PySchools - 主题1 问题7
我在PySchools.com上练习Python的时候遇到了一些问题,卡住了。我相信这只是个简单的任务。有人能帮忙吗?
你可以使用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 =
3 个回答
0
greetings = "Hello Google" repeated_greetings = len(greetings) * greetings print repeated_greetings
> '你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌你好,谷歌'
1
你可以通过将字符数量乘以问候语来简单实现这个功能。
greetings = number_of_char * greeting
2
你可以把一个字符串和一个整数相乘,这样就可以把这个字符串重复多次。
>>> 'abc' * 2
'abcabc'