{"id":9792,"date":"2021-09-30T17:30:17","date_gmt":"2021-09-30T12:00:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=9792"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-reverse-a-string-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-reverse-a-string-using-recursion\/","title":{"rendered":"Python Program to Reverse a String Using Recursion"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Recursion in Python:<\/strong><\/p>\n Python also supports function recursion, which means that a specified function can call itself.<\/p>\n Recursion is a mathematical and programming concept that is widely used. It signifies that a function calls itself. This has the advantage of allowing you to loop through data to obtain a result.<\/p>\n The developer must exercise extreme caution when using recursion since it is quite easy to write a function that never terminates or consumes excessive amounts of memory or computing power. However, when performed correctly, recursion may be a tremendously efficient and mathematically elegant way to programming.<\/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 a given string using recursion in Python.<\/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 Related Programs<\/strong>:<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts. Recursion in Python: Python also supports function recursion, which means that a specified function can call itself. Recursion is a mathematical and programming concept that is widely used. …<\/p>\ngiven string = btechgeeks<\/pre>\n
The modified given string{after reversing} = skeeghcetb<\/pre>\n
The original given string = aplustopper<\/pre>\n
The modified given string{after reversing} = reppotsulpa<\/pre>\n
Program to Reverse a String Using Recursion in Python<\/h2>\n
\n
1)Using Recursion(Static Input)<\/h3>\n
\n
# function which accepts the given string as an argument and\r\n# reverse the given string using recursion and return the reversed string\r\n\r\n\r\ndef reverseRecursion(given_string):\r\n # Calculate the length of the given string using the len() function.\r\n stringLen = len(given_string)\r\n # if len(str1) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If the length of the string is 1,\r\n # the string is returned, otherwise, the function is called recursively.\r\n if stringLen == 1:\r\n return given_string\r\n else:\r\n # The slice operator will slice the string and concatenate it to the end of the\r\n # slice string if it anticipates the first character.\r\n return reverseRecursion(given_string[1:]) + given_string[0]\r\n\r\n\r\n# Give the string from the user as static input and store it in a variable.\r\ngivenstring = 'btechgeeks'\r\n# printing the original given string\r\nprint('The original given string =', givenstring)\r\n# passing the given string as an argument to the recursive function\r\n# 'reverseRecursion' which reverses the given string.\r\n\r\nprint('The modified given string{after reversing} = ',\r\n reverseRecursion(givenstring))\r\n<\/pre>\n
The original given string = btechgeeks\r\nThe modified given string{after reversing} = skeeghcetb<\/pre>\n
2)Using Recursion(User Input)<\/h3>\n
\n
# function which accepts the given string as an argument and\r\n# reverse the given string using recursion and return the reversed string\r\n\r\n\r\ndef reverseRecursion(given_string):\r\n # Calculate the length of the given string using the len() function.\r\n stringLen = len(given_string)\r\n # if len(str1) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If the length of the string is 1,\r\n # the string is returned, otherwise, the function is called recursively.\r\n if stringLen == 1:\r\n return given_string\r\n else:\r\n # The slice operator will slice the string and concatenate it to the end of the\r\n # slice string if it anticipates the first character.\r\n return reverseRecursion(given_string[1:]) + given_string[0]\r\n\r\n\r\n# Scan the given string from the user as user input using the int(input()) function.able.\r\ngivenstring = input('Enter some random string = ')\r\n# printing the original given string\r\nprint('The original given string =', givenstring)\r\n# passing the given string as an argument to the recursive function\r\n# 'reverseRecursion' which reverses the given string.\r\n\r\nprint('The modified given string{after reversing} = ',\r\n reverseRecursion(givenstring))\r\n<\/pre>\n
Enter some random string = aplustopper\r\nThe original given string = aplustopper\r\nThe modified given string{after reversing} = reppotsulpa<\/pre>\n
\n