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)
Output
['Python', 'is', 'a', 'programming', 'language']
Python is a programming language
In the above code example
- Created a variable named input_str that contains a string with multiple words.
- Created a variable called n and assigned the integer 5 to it.
- 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").
- Then we have created a string called result_str, which is the concatenation of the elements of result_list, separated by spaces.
- Finally, we print the contents of result_list and result_str respectively.
To understand the above code example, you can read the below articles:
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:
- We create a string, my_str.
- We create a list named parts, by splitting my_str into its component words.
- We take the first three words from parts.
- We create a new string, result, by joining together the words from the first three elements of parts.
- 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:
- The above code imports the re module, which provides functions for working with regular expressions.
- my_str is a string containing the phrase "Programming is fun and interesting".
- 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.
- The [:3] slice notation returns the first three elements of the list returned by findall().
- 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.
- Finally, the result is printed to the console.
- Python - Get first word form a string
- Python code to get the first character from a string
- Get first N characters from a string in Python
- Capitalize first letter of a string in Python (5 Solutions)
- Get first record Sqlalchemy
- First highest views based recommendation system in python part 1
- first python code