- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have sentence s where some English words are present, that are separated by a single space with no leading or trailing spaces. We also have another value k. We have to find only the first k words after truncating.

So, if the input is like s = "Coding challenges are really helpful for students" k = 5, then the output will be True (See the image)

To solve this, we will follow these steps −

words := split s by spaces

join first k letters from words array by separating spaces and return

Let us see the following implementation to get better understanding −

def solve(s, k): words = s.split() return " ".join(words[:k]) s = "Coding challenges are really helpful for students" k = 5 print(solve(s, k))

"Coding challenges are really helpful for students", 5

Coding challenges are really helpful

- Related Questions & Answers
- Python program to split string into k distinct partitions
- Program to find least number of unique integers after K removals using Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find most occurring number after k increments in python
- Program to find minimum amplitude after deleting K elements in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find state of prison cells after k days in python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Truncating multiple strings after 100 characters in ABAP
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find sum of digits in base K using Python
- Find all possible substrings after deleting k characters in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python

Advertisements