Sentence Smash
Instructions
Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!
Example
['hello', 'world', 'this', 'is', 'great'] => 'hello world this is great'
Solution
def smash ( words ):
return " ".join(words)
In Python, .join is a string method that takes an iterable (such as a list or a tuple) as an argument and returns a string that is the concatenation of all the elements in the iterable, separated by the string on which the method is called. For example,
"-".join(["a", "b", "c"])returns
"a-b-c"
iterable:可迭代对象
separated by the string on which the method is called: 用调用方法的字符串作为分隔符
- separated by the string
- the method is called on the string
- on which的which指的是调用.join方法的字符串,也就是分隔符。
例如,如果你写"-" .join(["a", "b", "c"]),那么返回的字符串就是"a-b-c",其中"-"就是which指代的对象。
人生便是艺术。

浙公网安备 33010602011771号