In this python function program, we will reverse a string.
String: A string is any series of characters that are interpreted literally by a script. For example, “Python”.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function rev
- Use the def keyword to define the function.
- Pass a parameter i.e. a string to the function.
- Reverse the string using indexing.
- Print the output.
- Take a string as input through the user and pass it to the function while calling the function.
def rev(str1):
a = str1[::-1]
print("Reverse of the given string: ",a)
string = input("Enter a string: ")
rev(string)
Output :
Enter a string: Python1234
Reverse of the given string: 4321nohtyP