You can also do it with recursion:
def reverse(text):
if len(text) <= 1:
return text
return reverse(text[1:]) + text[0]
And a simple example for the string hello
:
reverse(hello)
= reverse(ello) + h # The recursive step
= reverse(llo) + e + h
= reverse(lo) + l + e + h
= reverse(o) + l + l + e + h # Base case
= o + l + l + e + h
= olleh
Related Contents:
- How to get a function name as a string?
- I have a string whose content is a function name, how to refer to the corresponding function in Python?
- Setting variables with exec inside a function
- Check presence of vowels in a string
- Best way to loop over a python string backwards
- What does the ‘b’ character do in front of a string literal?
- How do I execute a string containing Python code in Python?
- How do I get a substring of a string in Python?
- How do I get the filename without the extension from a path in Python?
- How to extract the substring between two markers?
- What is the difference between a string and a byte string?
- Finding all possible permutations of a given string in python
- Why are empty strings returned in split() results?
- String concatenation without ‘+’ operator
- Check if string ends with one of the strings from a list
- How do I split a multi-line string into multiple lines?
- Cannot concatenate ‘str’ and ‘float’ objects?
- How do I create a Python function with optional arguments?
- How does a Python for loop with iterable work?
- Why are str.count(”) and len(str) giving different output?
- Efficient way to add spaces between characters in a string
- str.strip() strange behavior
- How to convert an int to a hex string?
- How do I call a function from another .py file?
- Finding a substring within a list in Python [duplicate]
- Run PowerShell function from Python script
- Python lambda doesn’t remember argument in for loop [duplicate]
- How to write very long string that conforms with PEP8 and prevent E501 [duplicate]
- Count number of words per row
- Run length encoding in Python
- How to convert a string with comma-delimited items to a list in Python?
- In Python, when should I use a function instead of a method?
- What is a clean way to convert a string percent to a float?
- Difference between using commas, concatenation, and string formatters in Python
- How can I extract keywords from a Python format string?
- for loops and iterating through lists – how does “for a[-1] in a:” work?
- Remove final character from string
- Why doesn’t modifying the iteration variable affect subsequent iterations?
- How to explain the str.maketrans function in Python 3.6?
- How to apply a function to each sublist of a list in python?
- How to have multiple conditions for one if statement in python [duplicate]
- How do I check if a given Python string is a substring of another one? [duplicate]
- How to split a Python string on new line characters [duplicate]
- pandas: replace string with another string
- How to increment variable names/Is this a bad idea [duplicate]
- Determine precision and scale of particular number in Python
- python pandas- apply function with two arguments to columns
- Adding a column in pandas df using a function
- pandas combine two strings ignore nan values
- Sort a list of tuples by second value, reverse=True and then by key, reverse=False