Loading

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指代的对象。
posted @ 2023-03-13 16:02  Artwalker  阅读(33)  评论(0)    收藏  举报
Live2D