The reversed () function in Python
The reversed () function is one of the built-in functions in Python, used to reverse the original string returning the iterator. What syntax is the reversed () function, and what are the parameters, let's find out in this article Quantrimang.

The syntax of the reversed () function in Python
reversed(seq)
Parameters of the reversed () function:
Reversed () has only one parameter:
seq
: the string you want to reverse.
Strings are objects that support string protocols like __len __ () and __getitem __ () methods. For example: tuple, string, list, range .
Can use reversed () with any object implementing __reverse __ ()
Return value from (reversed)
The reversed () function allows us to process the items in the reverse order of the original string, receive the string and return an iterator with the reversed value of the original string passed.
Example 1: Using reversed () with string, tuple, list and range
print( reversed([44, 11, -90, 55, 3]) ) # với string seq_string = 'Python' print(list(reversed(seq_string))) # với tuple seq_tuple = ('P', 'y', 't', 'h', 'o', 'n') print(list(reversed(seq_tuple))) # với range seq_range = range(5, 9) print(list(reversed(seq_range))) # với list seq_list = [1, 2, 4, 3, 5] print(list(reversed(seq_list)))
Running the program, the result is returned:
['n', 'o', 'h', 't', 'y', 'P'] ['n', 'o', 'h', 't', 'y', 'P'] [8, 7, 6, 5] [5, 3, 4, 2, 1]
In the above example, we are converting the result of reversed () to list using the list () function.
Example 2: reversed () with custom objects
class Vowels: vowels = ['a', 'e', 'i', 'o', 'u'] def __reversed__(self): return reversed(self.vowels) v = Vowels() print(list(reversed(v)))
Running the program, the result is returned:
['u', 'o', 'i', 'e', 'a']
See also: Python built-in functions.
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
- The compile () function in Pythonthe compile () function returns a code object in python from the specified source. so how to use compile () function? please find out in this article.