How to get the Nth word in a given string using Python?

WBOY
Release: 2023-08-29 19:41:03
forward
1218 people have browsed it

How to get the Nth word in a given string using Python?

We can get the Nth Word in a Given String in Python using string splitting, regular expressions, split() method, etc. Manipulating strings is a common task in programming, and extracting specific words from a string can be particularly useful in various scenarios. In this article, we will explore different methods to extract the Nth word from a given string using Python.

Method 1: String splitting

This method involves splitting the string into a list of words and accessing the desired word based on the index.

Syntax

words = string.split()
Copy after login

Here, the split() method splits the string based on whitespace by default. The resulting words are stored in the words list.

algorithm

  • Accept the input string and the value of N.

  • Use the split() method to split the string into a list of words.

  • Access the Nth word from the list using indexing.

  • Access the Nth word from the list using indexing.

Example

Let us consider the following string: "The quick brown fox jumps over the lazy dog." In the following example, the input string is split into a list of words using the split() method. Since the value of N is 3, the function returns the third word, "brown".

def get_nth_word_split(string, n): words = string.split() if 1 <= n <= len(words): return words[n-1] else: return "Invalid value of N." # Test the function input_string = "The quick brown fox jumps over the lazy dog." n = 3 print(get_nth_word_split(input_string, n))
Copy after login

Output

brown
Copy after login

Method 2: Use regular expressions

Another method to extract the Nth word involves using regular expressions. This approach provides flexibility in handling different word patterns and delimiters.

Syntax

import re words = re.findall(pattern, string)
Copy after login

Here, use there.findall()function from the re module to extract all words from the input string based on the specified pattern. Patterns are defined using regular expressions. The extracted words are stored in the words list.

algorithm

  • Import re module.

  • Accept the input string and the value of N.

  • Define a regular expression pattern to match words.

  • Use the findall() function from the re module to extract all words from the string.

  • Access the Nth word from the list obtained in step 4 using indexing.

  • Return the Nth word.

Example

In the following example, the regular expression pattern '\w' is used to match all words in the input string. The findall() function extracts all words and stores them in a list. Since the value of N is 4, the function returns the fourth word "jumps".

import re def get_nth_word_regex(string, n): pattern = r'\w+' words = re.findall(pattern, string) if 1 <= n <= len(words): return words[n-1] else: return "Invalid value of N." # Test the function input_string = "The quick brown fox jumps over the lazy dog." n = 4 print(get_nth_word_regex(input_string, n))
Copy after login

Output

fox
Copy after login

Method 3: Use split() method and custom separator

In some cases, the string may have a specific delimiter other than whitespace. In such scenarios, we can modify the first method by specifying a custom delimiter for splitting the string.

Syntax

words = string.split(delimiter)
Copy after login

Here, thesplit()method is used to split the input string into a list of words based on a custom delimiter. The delimiter is passed as a parameter to the split() method. The split words are stored in the words list.

algorithm

  • Accept the input string, the delimiter, and the value of N.

  • Split the string into a list of words using the split() method and a custom delimiter.

  • Access the Nth word from the list using indexing

  • Return the Nth word.

Example

In the example below, the string is split into words using a custom delimiter (comma ","). The second word "banana" is extracted and returned because it corresponds to a value of N of 2.

def get_nth_word_custom_delimiter(string, delimiter, n): words = string.split(delimiter) if 1 <= n <= len(words): return words[n-1] else: return "Invalid value of N." # Test the function input_string = "apple,banana,orange,mango" delimiter = "," n = 2 print(get_nth_word_custom_delimiter(input_string, delimiter, n))
Copy after login

Output

banana
Copy after login

Conclusion

In this article, we have discussed how to get the nth word in a given string with the help of splitting string, using regular expressions and using splitting method with custom delimiter. Each method provides flexibility based on the requirements of the task. By understanding and implementing these techniques, you can efficiently extract specific words from strings in your Python program.

The above is the detailed content of How to get the Nth word in a given string using Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!