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:Splitting the String

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

Syntax

words = string.split()

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's consider the following string: "The quick brown fox jumps over the lazy dog." In the below 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))

Output

brown

Method 2:Using 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)

Here, re.findall() function from the re module is used to extract all words from the input string based on a specified pattern. The pattern is defined using regular expressions. The resulting words are stored in the words list.

Algorithm

  • Import the 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 below example, the regular expression pattern '\w+' is used to match all words in the input string. The findall() function extracts all the 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))

Output

fox

Method 3:Using the split() Method with Custom Delimiter

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)

Here, split() method is used to split the input string into a list of words based on a custom delimiter. The delimiter is specified as an argument to the split() method. The resulting words are stored in the words list.

Algorithm

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

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

  • Access the Nth word from the list using indexing

  • Return the Nth word.

Example

In the below example, the string is split into words using the custom delimiter, which is a comma (","). The second word, "banana", is extracted and returned as it corresponds to the value of N being 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))

Output

banana

Conclusion

In this article, we discussed how we can get the nth word in a given string with the help of methods like splitting the string, using regular expressions, and using the split method with a custom delimiter. Each method offers flexibility depending on the requirements of the task at hand. By understanding and implementing these techniques, you can efficiently extract specific words from strings in your Python programs.

Updated on: 18-Jul-2023

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements