Search code snippets, questions, articles...

Get first n words from a string in Python

If you're working with strings in Python, there may be times when you need to get only the first n words from a string. This can be easily done with a few lines of Python code. In this article, we'll show you how to get the first n words from a string in Python.
input_str = "Python is a programming language and we can develop applications using it"
n = 5

result_list = input_str.split()[:n]
result_str = " ".join(result_list)

print(result_list)
print(result_str)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

['Python', 'is', 'a', 'programming', 'language']

Python is a programming language

In the above code example

  1. Created a variable named input_str that contains a string with multiple words.
  2. Created a variable called n and assigned the integer 5 to it.
  3. The third line creates a list called result_list, which contains the first 5 elements of the input_str list (i.e. the words "Python", "is", "a", "programming" and "language").
  4. Then we have created a string called result_str, which is the concatenation of the elements of result_list, separated by spaces.
  5. Finally, we print the contents of result_list and result_str respectively.

To understand the above code example, you can read the below articles:

Convert a list to string

Split string to list in Python

Method 1: Using split() and join() function

Python has many built-in functions that allow you to manipulate strings. Two of these functions are the split() and join() functions. The split() function allows you to split a string into a list of substrings. The join() function allows you to join a list of strings into a single string. These two functions can be used together to get the first n words from a string.

my_str = "Programming is fun and interesting"

parts = my_str.split()[:3]
result = " ".join(parts)

print(result)

Output

Programming is fun

In the above code example:

  1.  We create a string, my_str.
  2. We create a list named parts, by splitting my_str into its component words.
  3. We take the first three words from parts.
  4. We create a new string, result, by joining together the words from the first three elements of parts.
  5. We print the result variable to the console.

Using regex re.findall() and join() function

We can use the re.findall() function in python to get the first n words in a given string. This function returns a list of all the matches in the given string. We can then use the join() function to join all the words in the list and return a string.

import re

my_str = "Programming is fun and interesting"
result = re.findall(r'\w+', my_str)[:3]
result = " ".join(result)

print(result)

Output

Programming is fun

Explanation:

  1. The above code imports the re module, which provides functions for working with regular expressions.
  2. my_str is a string containing the phrase "Programming is fun and interesting".
  3. re.findall() is a function that finds all substrings in a string that match a given regular expression. In this case, the regular expression is r'\w+', which matches any sequence of one or more alphanumeric characters. The findall() function returns a list of all matches found.
  4. The [:3] slice notation returns the first three elements of the list returned by findall().
  5. The join() method concatenates the elements of a list, separated by a given string. In this case, the list returned by findall() is joined by a space character.
  6. Finally, the result is printed to the console. 
Was this helpful?
0 Comments