{"id":18720,"date":"2021-08-26T10:38:12","date_gmt":"2021-08-26T05:08:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=18720"},"modified":"2021-11-22T18:37:18","modified_gmt":"2021-11-22T13:07:18","slug":"python-program-to-reverse-the-order-of-palindrome-words-in-a-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-reverse-the-order-of-palindrome-words-in-a-sentence\/","title":{"rendered":"Python Program to Reverse the Order of Palindrome Words in a Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Sort Palindrome Words in a Sentence<\/a> Palindrome:<\/strong><\/p>\n If the reverse of the given string is the same as the given original string, it is said to be a palindrome.<\/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 reverse the order of all the palindromic words in a given 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.<\/strong><\/p>\n In the previous article, we have discussed Python Program to Sort Palindrome Words in a Sentence Given a string and the task is to reverse the order of all the palindromic words in a given sentence in Python Palindrome: If the reverse of the given string is the same as the given original string, it …<\/p>\n
\nGiven a string and the task is to reverse the order of all the palindromic words in a given sentence in Python<\/p>\nGiven string\/sentence = 'good bob how are you dod mom'<\/pre>\n
The given string before reversing the order of paindromic words is = good bob how are you dod mom\r\nThe given string after reversing the order of paindromic words is = good mom how are you dod bob<\/pre>\n
Given string\/sentence = 'hello madam how are yoou mom'<\/pre>\n
The given string before reversing the order of paindromic words is = hello madam how are yoou mom\r\nThe given string after reversing the order of paindromic words is = hello mom how are yoou madam<\/pre>\n
Program to Reverse the Order of Palindrome Words in a Sentence in Python<\/h2>\n
\n
Method #1: Using reverse() function (Static input)<\/h3>\n
\n
# Give the sentence\/string as static input and store it in a variable.\r\ngvnstrng = 'good bob how are you dod mom'\r\n# Convert the given sentence to a list of words using list()\r\n# and split() functions and store it another variable.\r\nstrngwrdslst = list(gvnstrng.split())\r\n# Take an empty list to say palindromicwordslist\r\n# that stores all the palindromic words in the given string\r\n# and initialize it to null\/empty using the list() function or [].\r\npalindromicwordslist = []\r\n# Traverse the given list of words using a for loop.\r\nfor wrd in strngwrdslst:\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(wrd == wrd[::-1]):\r\n # If it is true then append this word to the palindromicwordslist\r\n # using the append() function.\r\n palindromicwordslist.append(wrd)\r\n\r\n# Reverse the palindromicwordslist using the reverse() function.\r\npalindromicwordslist.reverse()\r\n# Take a variable say tempo and initialize its value to 0\r\n# (Here it acts as a pointer to palindromicwordslist ).\r\ntempo = 0\r\n# Traverse the list of words of the given sentence using the For loop.\r\nfor wrditr in range(len(strngwrdslst)):\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):\r\n # If it is true then modify the word with the palindromicwordslist[tempo] word.\r\n strngwrdslst[wrditr] = palindromicwordslist[tempo]\r\n tempo = tempo+1\r\n # Increment the tempo value by 1.\r\n\r\n\r\n# Convert this list of words of the given sentence\r\n# to the string using the join() function.\r\nfinalstrng = ' '.join(strngwrdslst)\r\nprint('The given string before reversing the order of paindromic words is = ', gvnstrng)\r\n# Print the final string after reversing the palindromic words.\r\nprint('The given string after reversing the order of paindromic words is = ', finalstrng)\r\n<\/pre>\n
The given string before reversing the order of paindromic words is = good bob how are you dod mom\r\nThe given string after reversing the order of paindromic words is = good mom how are you dod bob<\/pre>\n
Method #2: Using reverse() function (User input)<\/h3>\n
\n
# Give the sentence\/string as user input using input() function\r\n# and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Convert the given sentence to a list of words using list()\r\n# and split() functions and store it another variable.\r\nstrngwrdslst = list(gvnstrng.split())\r\n# Take an empty list to say palindromicwordslist\r\n# that stores all the palindromic words in the given string\r\n# and initialize it to null\/empty using the list() function or [].\r\npalindromicwordslist = []\r\n# Traverse the given list of words using a for loop.\r\nfor wrd in strngwrdslst:\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(wrd == wrd[::-1]):\r\n # If it is true then append this word to the palindromicwordslist\r\n # using the append() function.\r\n palindromicwordslist.append(wrd)\r\n\r\n\r\n# Reverse the palindromicwordslist using the reverse() function.\r\npalindromicwordslist.reverse()\r\n# Take a variable say tempo and initialize its value to 0\r\n# (Here it acts as a pointer to palindromicwordslist ).\r\ntempo = 0\r\n# Traverse the list of words of the given sentence using the For loop.\r\nfor wrditr in range(len(strngwrdslst)):\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):\r\n # If it is true then modify the word with the palindromicwordslist[tempo] word.\r\n strngwrdslst[wrditr] = palindromicwordslist[tempo]\r\n tempo = tempo+1\r\n # Increment the tempo value by 1.\r\n\r\n\r\n# Convert this list of words of the given sentence\r\n# to the string using the join() function.\r\nfinalstrng = ' '.join(strngwrdslst)\r\nprint('The given string before reversing the order of paindromic words is = ', gvnstrng)\r\n# Print the final string after reversing the palindromic words.\r\nprint('The given string after reversing the order of paindromic words is = ', finalstrng)\r\n<\/pre>\n
Enter some random string = hello madam how are yoou mom\r\nThe given string before reversing the order of paindromic words is = hello madam how are yoou mom\r\nThe given string after reversing the order of paindromic words is = hello mom how are yoou madam<\/pre>\n
\n