Python - Get Nth word in given String
Last Updated :
23 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to get the Nth word of a String. This kind of problem has many application in school and day-day programming. Let's discuss certain ways in which this problem can be solved.
Method #1 : Using loop This is one way in which this problem can be solved. In this, we run a loop and check for spaces. The Nth word is when there is N-1th space. We return that word.
Python3
# Python3 code to demonstrate working of
# Get Nth word in String
# using loop
# initializing string
test_str = "GFG is for Geeks"
# printing original string
print("The original string is : " + test_str)
# initializing N
N = 3
# Get Nth word in String
# using loop
count = 0
res = ""
for ele in test_str:
if ele == ' ':
count = count + 1
if count == N:
break
res = ""
else :
res = res + ele
# printing result
print("The Nth word in String : " + res)
Output : The original string is : GFG is for Geeks
The Nth word in String : for
Method #2 : Using split() This is a shorthand with the help of which this problem can be solved. In this, we split the string into a list and then return the Nth occurring element.
Python3
# Python3 code to demonstrate working of
# Get Nth word in String
# using split()
# initializing string
test_str = "GFG is for Geeks"
# printing original string
print("The original string is : " + test_str)
# initializing N
N = 3
# Get Nth word in String
# using split()
res = test_str.split(' ')[N-1]
# printing result
print("The Nth word in String : " + res)
Output : The original string is : GFG is for Geeks
The Nth word in String : for
Method #3 : Using re.findall()
This approach uses the re.findall() function from the re library. It searches for all non-whitespace substrings in the given string and returns them in a list. The Nth word can then be accessed by indexing the list.
Python3
import re
# initializing string
test_str = "GFG is for Geeks"
# printing original string
print("The original string is : " + test_str)
# initializing N
N = 3
# Get Nth word in String
# using re.findall()
res = re.findall(r'\S+', test_str)
# printing result
print("The Nth word in String : " + res[N-1])
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original string is : GFG is for Geeks
The Nth word in String : for
Time complexity is O(n) and Auxiliary Space is O(n).
Method 4 : using the string slicing method.
step-by-step approach
- Initialize the string.
- Print the original string.
- Initialize the value of N.
- Find the starting and ending indices of the Nth word using the string slicing method.
- Extract the Nth word using the starting and ending indices.
- Print the Nth word.
Python3
# initializing string
test_str = "GFG is for Geeks"
# printing original string
print("The original string is : " + test_str)
# initializing N
N = 3
# Get Nth word in String
# using slicing
start = 0
end = len(test_str)
for i in range(N-1):
start = test_str.find(" ", start) + 1
for i in range(start, len(test_str)):
if test_str[i] == " ":
end = i
break
nth_word = test_str[start:end]
# printing result
print("The Nth word in String : " + nth_word)
OutputThe original string is : GFG is for Geeks
The Nth word in String : for
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Similar Reads
Python | Extract Nth words in Strings List Sometimes, while working with Python Lists, we can have problems in which we need to perform the task of extracting Nth word of each string in List. This can have applications in the web-development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using list compreh
7 min read
Python - Words Lengths in String We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List
2 min read
Word location in String - Python Word location in String problem in Python involves finding the position of a specific word or substring within a given string. This problem can be approached using various methods in Python, such as using the find(), index() methods or by regular expressions with the re module.Using str.find()str.fi
4 min read
Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
4 min read
Python | Extract odd length words in String Sometimes, while working with Python, we can have a problem in which we need to extract certain length words from a string. This can be extraction of odd length words from the string. This can have application in many domains including day-day programming. Lets discuss certain ways in which this tas
5 min read