Ctrl K

Keep the First Words

Easy
The string text is a few words made of English letters (lowercase and uppercase) separated by single spaces, with no spaces at the start or the end. Cut it down so that only the first count words remain, and return the result. The remaining words are still separated by single spaces, and there must be no trailing space.
Example 1
text="Meet me at the old bridge tonight", count=3 → 'Meet me at'

The first three words are "Meet", "me" and "at"; the rest is dropped.

Example 2
text="Short note", count=2 → 'Short note'

The text has exactly two words, so it comes back unchanged.

Constraints
  • 1 ≤ len(text) ≤ 500
  • text consists of English letters and single spaces between words
  • 1 ≤ count ≤ number of words in text
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
text="Meet me at the old bridge tonight", count=3
Expected
'Meet me at'

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.