Program to Print Nth word in a given String

Python Program to Print Nth word in a given String

In this article, we will look at how to find the Nth word in a given text using Python.
We frequently encounter circumstances in which we do not require the complete string but merely a certain word from it.

Given multiple strings separated by spaces, the task is to print the Nth word in the given string in Python.

Examples:

Example1:

Input:

Given string =Good morning this is btechgeeks platform
Given n=3

Output:

The [ 3 ] th word in the given Good morning this is btechgeeks platform is { this }

Example2:

Input:

Given string =hello thi sis btechgeeks
Given n=4

Output:

The [ 4 ] th word in the given hello thi sis btechgeeks is { btechgeeks }

Program to Print Nth word in a given String in Python

Below are the ways to print the Nth word in the given string in Python.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Method #1: Using split() Function (Static Input)

Approach:

  • Give the string as static input and store it in the variable.
  • Give the value of n as static input and store it in another variable.
  • Convert this string into a list of words and split them with spaces using split() and list() functions.
  • Print the nth word in the given list by printing the n-1 indexed element in the above list.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in the variable.
gvnwordsstring = 'Good morning this is btechgeeks platform'
# Give the value of n as static input and store it in another variable.
p = 3
# Convert this string into a list of words and
# split them with spaces using split() and list() functions.
stringwordslist = list(gvnwordsstring.split())

# Print the nth word in the given list by printing the n-1 indexed element
# in the above list.
nthword = stringwordslist[p-1]
print('The [', p, '] th word in the given',
      gvnwordsstring, 'is {', nthword, '}')

Output:

The [ 3 ] th word in the given Good morning this is btechgeeks platform is { this }

Method #2: Using split() Function (User Input)

Approach:

  • Give the string as user input using the input() function and store it in the variable.
  • Give the value of n as user input using the int(input()) function and store it in another variable.
  • Convert this string into a list of words and split them with spaces using split() and list() functions.
  • Print the nth word in the given list by printing the n-1 indexed element in the above list.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in the variable.
gvnwordsstring = input('Enter some random string = ')
# Give the value of n as user input using the int(input())
# function and store it in another variable.
p = int(input('Enter some random value of n ='))
# Convert this string into a list of words and
# split them with spaces using split() and list() functions.
stringwordslist = list(gvnwordsstring.split())

# Print the nth word in the given list by printing the n-1 indexed element
# in the above list.
nthword = stringwordslist[p-1]
print('The [', p, '] th word in the given',
      gvnwordsstring, 'is {', nthword, '}')

Output:

Enter some random string = hello this is btechgeeks
Enter some random value of n =4
The [ 4 ] th word in the given hello thi sis btechgeeks is { btechgeeks }

Related Programs: