{"id":14307,"date":"2021-09-30T12:30:02","date_gmt":"2021-09-30T07:00:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=14307"},"modified":"2021-11-22T18:34:38","modified_gmt":"2021-11-22T13:04:38","slug":"python-program-to-print-nth-word-in-a-given-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-nth-word-in-a-given-string\/","title":{"rendered":"Python Program to Print Nth word in a given String"},"content":{"rendered":"
In this article, we will look at how to find the Nth word in a given text using Python.
\nWe frequently encounter circumstances in which we do not require the complete string but merely a certain word from it.<\/p>\n
Given multiple strings separated by spaces, the task is to print the Nth word in the given string in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print the Nth word in the given string in Python.<\/p>\n Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation: Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven string =Good morning this is btechgeeks platform\r\nGiven n=3<\/pre>\n
The [ 3 ] th word in the given Good morning this is btechgeeks platform is { this }<\/pre>\n
Given string =hello thi sis btechgeeks\r\nGiven n=4<\/pre>\n
The [ 4 ] th word in the given hello thi sis btechgeeks is { btechgeeks }<\/pre>\n
Program to Print Nth word in a given String in Python<\/h2>\n
\n
Method #1: Using split() Function (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in the variable.\r\ngvnwordsstring = 'Good morning this is btechgeeks platform'\r\n# Give the value of n as static input and store it in another variable.\r\np = 3\r\n# Convert this string into a list of words and\r\n# split them with spaces using split() and list() functions.\r\nstringwordslist = list(gvnwordsstring.split())\r\n\r\n# Print the nth word in the given list by printing the n-1 indexed element\r\n# in the above list.\r\nnthword = stringwordslist[p-1]\r\nprint('The [', p, '] th word in the given',\r\n gvnwordsstring, 'is {', nthword, '}')\r\n<\/pre>\n
The [ 3 ] th word in the given Good morning this is btechgeeks platform is { this }<\/pre>\n
Method #2: Using split() Function (User Input)<\/h3>\n
\n
\n<\/strong><\/p>\n# Give the string as user input using the input() function and store it in the variable.\r\ngvnwordsstring = input('Enter some random string = ')\r\n# Give the value of n as user input using the int(input())\r\n# function and store it in another variable.\r\np = int(input('Enter some random value of n ='))\r\n# Convert this string into a list of words and\r\n# split them with spaces using split() and list() functions.\r\nstringwordslist = list(gvnwordsstring.split())\r\n\r\n# Print the nth word in the given list by printing the n-1 indexed element\r\n# in the above list.\r\nnthword = stringwordslist[p-1]\r\nprint('The [', p, '] th word in the given',\r\n gvnwordsstring, 'is {', nthword, '}')\r\n<\/pre>\n
Enter some random string = hello this is btechgeeks\r\nEnter some random value of n =4\r\nThe [ 4 ] th word in the given hello thi sis btechgeeks is { btechgeeks }<\/pre>\n
\n