Get solution of write a python program to reverse a string

Get solution of write a python program to reverse a string : The reverse function is no reverse function present in the string class. There are many ways to reverse the string. 1)Using loop () function is used to join the function with reversed iteration. 2) Slicing used to create the reverse the copy of the string. 3) Using the recursive function() and reversed. 4) For and while () appending characters in reverse order and iterate, append them. 5) The build in function is not present in the reverse string of python language.

Step1:-

  1. Take the string from user
  2. Then use this string for slicing to reverse the string.
  3. Print the reversed string
  4. Exit the program.

Step2:-

  1. The creation of loop to read each character of the string one by one.
  2. Declare the empty string
  3. Concatenation of read character at beginning of string.

1) Reverse a string in python using for loop :-

def reverse(s):
str=””
for i in s:
str=i+str
return str
s=”WelcomePython”
print(“The original string is:” end=””)
print(s)
print(“The reversed string (using loops) is:” end=””)
print(reverse(s))
Output:-
The original string is: WelcomePython
The reversed string (using loops) is: nohtyPemocleW
The code above gives the reversed string at output and iterates to the every element and joins each character in beginning to obtain the reversed string.

2) Reverse a string Using Stack:-

The empty stack is created and one by one character string is pushed to stack.
All the character is altered from stack are pops and puts back to the string.

def createStack ():
stack= []
return stack
def size (stack):
return len (stack)
                      def isEmpty (stack):
if size (stack) ==0:
return true
def push (stack, item):
stack. append (item)
def pop (stack):
if isEmpty (stack): return
return stack.pop ()
def reverse (string):
n=len (string)
stack=createStack ()
for i in range (0, n, 1):
push (stack, string[i])
           string=””
for i in range (0, n, 1):
string+=pop (stack)
return string
s=”Python”
print(“The original string is:” end=””)
print(s)
print(“The reversed string is:” end=””)
print(reverse(s))
Output:-
The original string is: Python
The reversed string (using stack) is: nohtyP

3) Reverse String Using Slice:-

The sting is the sequence of the protocol in the python language and support the feature called as slicing.
Program:-

def reverse (string):
string=string [::-1]
return string
s=”Python”
print(“The original string is:” end=””)
Print(s)
print(“The reversed string (using extended slice syntax) is:” end=””)
print(reverse(s)

 

Output:-
The original string is: Python
The reversed string (using stack) is: nohtyP
Program:-
txt=”Hello world”’[::-1]
print(txt)
The string text that will read the “Hello World” and then print the string and the result.
The creation of slice will start at end of the string and move backward. The slice element [::-1] means the start from the end and ends at 0 position .
The -1 is the negative one and its meaning is that step one backward.

4) Reverse String Using Reversed:-

It will return the reversed iteration of string and then elements are joined with an empty string and separated by join().
Program:-
def reverse (string):
string=””.join (reversed (string))
return string
s=”Python”
print(“The original string is:” end=””)
print(s)
print(“The reversed string (using reversed) is: end=””)

print(reverse(s))
Output:-
The original string is: Python
The reversed string (using stack) is: nohtyP

5) Reverse a string in python using recursive:-

The code has the string as an argument to the recursive function and reverses it.
The base condition is if the length of the string is equal to 0 the string is returned and if not equal to 0 the recursive function is called to slice the part of the string and concatenate the first character to end of the sliced string.
The program to reverse string using the recursive function as,
Program:-
def reverse(s):
if len(s) ==0:
return s
else:
return reverse(s [1:1]) +s [0]
s=”Welcome python”
print(“The original string is:” end=””)
print(reverse(s))

 

Output:-
                         The original string is: Welcome Python
             The reversed string is: nohtyPemocleW

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India