{"id":16147,"date":"2021-08-12T09:36:57","date_gmt":"2021-08-12T04:06:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=16147"},"modified":"2021-11-22T18:38:26","modified_gmt":"2021-11-22T13:08:26","slug":"python-program-to-reverse-each-word-in-a-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-reverse-each-word-in-a-sentence\/","title":{"rendered":"Python Program to Reverse each Word in a Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Get the Position of Max Value in a List<\/a> Split method:<\/strong><\/p>\n Splitting a string into a list is accomplished by the split() method. It is a built-in Function in python.<\/p>\n join function:<\/strong><\/p>\n The join() method is a string method that returns a string containing the elements of a sequence that have been joined by a string separator.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Reverse each Word in a Sentence.<\/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:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Get the Position of Max Value in a List Given a Sentence, the task is to Reverse each Word in a Given Sentence. Split method: Splitting a string into a list is accomplished by the split() method. It is a built-in Function in python. join …<\/p>\n
\nGiven a Sentence, the task is to Reverse each Word in a Given Sentence.<\/p>\nGiven String = Hello this is btechgeeks<\/pre>\n
The Reverse of each word of the above given sentence = olleH siht si skeeghcetb<\/pre>\n
Given String = Good Morning btechGeeks<\/pre>\n
The Reverse of each word of the above given sentence = dooG gninroM skeeGhcetb<\/pre>\n
Program to Reverse each Word in a Sentence<\/h2>\n
\n
Method #1: Using split() Method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\nstrng = \"Hello this is btechgeeks\"\r\n# Split the words of the given string to a list of words using the built-in split() function and store it in another variable.\r\nsplt_wrds = strng.split()\r\n# Traverse in the above Given List of words using For Loop.\r\n# Inside the Loop, Reverse each word of the list Using Slicing method and\r\n# store it in a variable.\r\nrversd_wrds = [wrd[::-1] for wrd in splt_wrds]\r\n# Join all the reversed words of a given sentence using the built-in join( ) function\r\n# and store it in another variable.\r\nfinl_sentnce = \" \".join(rversd_wrds)\r\n# Print the reversed Sentence of each word of a given String.\r\nprint(\"The Reverse of each word of the above given sentence = \", finl_sentnce)\r\n<\/pre>\n
The Reverse of each word of the above given sentence = olleH siht si skeeghcetb<\/pre>\n
Method #2: Using split() Method (User Input)<\/h3>\n
\n
# Give\u00a0the\u00a0string\u00a0as\u00a0User\u00a0input\u00a0using\u00a0input()\u00a0function\u00a0and\u00a0store\u00a0it\u00a0in\u00a0a\u00a0variable.\r\nstrng = input(\" Enter some random String = \")\r\n# Split the words of the given string to a list of words using the built-in split() function and store it in another variable.\r\nsplt_wrds = strng.split()\r\n# Traverse in the above Given List of words using For Loop.\r\n# Inside the Loop, Reverse each word of the list Using Slicing method and\r\n# store it in a variable.\r\nrversd_wrds = [wrd[::-1] for wrd in splt_wrds]\r\n# Join all the reversed words of a given sentence using the built-in join( ) function\r\n# and store it in another variable.\r\nfinl_sentnce = \" \".join(rversd_wrds)\r\n# Print the reversed Sentence of each word of a given String.\r\nprint(\"The Reverse of each word of the above given sentence = \", finl_sentnce)\r\n<\/pre>\n
Enter some random String = Good Morning btechGeeks\r\nThe Reverse of each word of the above given sentence = dooG gninroM skeeGhcetb<\/pre>\n
\n