All Python Programs Related to String Reverse

write a python function that returns the reverse of a string (i.e. the string backwards).

Explanation
===================
Define a function that accepts a string as input to the python function, just return the reverse of input string using string slicing method [::-1]

In the main program take a string variable named as test, using input() function call ask the user to enter a string.

Then using the print function, call print the reverse of a given string by calling the string_reverse() function.


Complete Python Program
=============================
def string_reverse(data):
   return data[::-1]


text = input("Enter a String : ")
print("Reverse of Given String is :", string_reverse(text))

Output
==========
Enter a String : python programming
Reverse of Given String is : gnimmargorp nohtyp


All Python Programs Related to String Reverse




write a python function to reverse a string if it's length is a multiple of 4 else print the string

Explanation
===================
Define a function that accepts a string as input to the python function. Calculate the length of the string and check if it's multiple of 4.

If the string length is multiple of 4 then return reverse of string else just simply return the given input string


Complete Python Program
=============================
def string_reverse_multiple4(data):
   if len(data) % 4 == 0:
       return data[::-1]
   else:
       return data


text = input("Enter a String : ")
print("New String is :", string_reverse_multiple4(text))



Output
==========
Enter a String : code
New String is : edoc


Output 2
============
Enter a String : string
New String is : string







write a python class to reverse a string word by word.

Explanation
===================
Define a class named string_class, inside the class define the constructor that takes string as parameter.

In the constructor initialize a string, Using another class member function returns the reverse of the given string.

In the main program ask the user to enter a string and create the string_class() object.

Using the string_class() object call the string reverse function and using print() function call print  the reverse of string


Complete Python Program
=============================

class string_class:

   def __init__(self, data):
       self.text = data

   def string_reverse(self):
       return self.text[::-1]


my_string = input("Enter a String : ")
string_object = string_class(my_string)
print("Reverse of Given String is :", string_object.string_reverse())


Output
==========
Enter a String : code with tj
Reverse of Given String is : jt htiw edoc







write a python program to print a string in reverse order using stack

Explanation
===================
Take a stack class and define four function
First function is constructor to initialize a empty list
Second function is to check if string is empty or not, it will return boolean value
Third function if to push the item in to stack, using append() a built in list function
Fourth Function to pop() and item from the stack

In the main file define a function to reverse a string, that takes a stack() object, keep pushing all the letters from string to stack, then take a while loop until stack is empty, keep popping the letters and append it to new_string.

In the main program, ask the user to enter a string using the input function and call the above defined function to finally print the return value of the function.

Complete Python Program
=============================
class Stack:
   def __init__(self):
       self.items = []

   def is_empty(self):
       return self.items == []

   def push(self, item):
       self.items.append(item)

   def pop(self):
       return self.items.pop()

def reverse_string(input_string):
   stack_object = Stack()
   for letter in input_string:
       stack_object.push(letter)

   text = ""
   while not stack_object.is_empty():
       text += stack_object.pop()
   return text


input_string = input("Enter a String to Reverse : ")
print("Reversed String using Stack is :", reverse_string(input_string))


Output
==========
Enter a String to Reverse : string reverse using stack
Reversed String using Stack is : kcats gnisu esrever gnirts







write a python code to reverse the given string using negative indexing

Explanation
===================
Take two string variables, ask the user to enter a string using the input function call. Second variable initializes it to an empty string.

Get the last index using len() function and negate it. Use the while loop to iterate from -string_length upto 0 and add each letter of index to reverse_string variable.

Keep incrementing the negative index until it reaches 0th index

Use print function to print the original string and reverse of the string.


Complete Python Program
=============================
input_string = input("Enter a String : ")
reversed_string = ""

index = -(len(input_string) - 1)
while index <= 0:
   reversed_string += input_string[-index]
   index += 1

print("Original string:", input_string)
print("Reversed string:", reversed_string)

Output
==========
Enter a String : hello, world!
Original string: hello, world!
Reversed string: !dlrow ,olleh





write a python function that reverse a string recursively

Explanation
===================
Define recursive function that takes input as string. In the recursive function use the condition to check if length of string is 0 then return empty string.

If the string length is not zero then take the last letter of the given string and call the recursive function by passing the given string removing the last character.

In the main program, take the string variable using the input function, call the user to enter the string, then call the recursive function and print the reverse of the string.

Complete Python Program
=============================
def recursion(text):
   if len(text) == 0:
       return ""
   else:
       return text[-1] + recursion(text[:-1])


data = input("Enter a String : ")
print("Reverse of string is : ", recursion(data))

Output
==========
Enter a String : hey how are u
Reverse of string is :  u era woh yeh







write a python program to reverse a string and print its alternate characters

Explanation
===================

Take a string variable named as text and use input function call to prompt the user to enter a string.

Use the string slicing method to reverse the string and store the reverse of string to new variable reverse_string

Use the for loop to iterate all the letters present in reverse_string and enumerate it to get index, check if index is even print the letters.


Complete Python Program
=============================
text = input("Enter a String : ")

reverse_string = text[::-1]
print("Reverse of string is : ", reverse_string)

print("Alternate Characters of Reversed String is : ", end="")
for index, letter in enumerate(reverse_string):
   if index % 2 == 0:
       print(letter, end="")



Output
==========
Enter a String : enter
Reverse of string is :  retne
Alternate Characters of Reversed String is : rte





write a python program to reverse a string using for loop

Explanation
===================
Ask the user to input a string using input() function call, take another string variable to store the reverse of the string.

Calculate the end index of given string using length and using for loop iterate from end index upto 0th index.

Add the indexed letter to the reverse_string, as the index gets iterated from negative string end to 0 the given string gets reversed.

Use the print function call to print the reverse of the string.


Complete Python Program
=============================
input_string = input("Enter a String : ")
reversed_string = ""

end = -(len(input_string) - 1)
for index in range(end, 1):
  reversed_string += input_string[index]

print("Original string:", input_string)
print("Reversed string:", reversed_string)


Output
==========
Enter a String : code
Original string: code
Reversed string: odec






write a python program to reverse a string using built in methods

Explanation
===================
Ask the user to enter a string and store it into a variable. Use the reversed() build in function to reverse the string and use join() to combine all the reversed letters.

Finally use print function to print the given string and reversed string.

Complete Python Program
=============================
input_string = input("Enter a String : ")
reversed_string = "".join(reversed(input_string))

print("Original string:", input_string)
print("Reversed string:", reversed_string)

Output
==========
Enter a String : i love python
Original string: i love python
Reversed string: nohtyp evol i





write a python program to reverse a string. input in= python output s_out= nohtyp

Or

write a python program to reverse a string. sample string 1234abcd output dcba4321

Explanation
===================
Ask the user to enter a string and store it into a variable, use the string slicing method to reverse a string and store the reversed string into another variable.

Use the print function call to print the given string and reversed string.


Complete Python Program
=============================
input_string = input("Enter a String : ")
reversed_string = input_string[::-1]

print("Original string:", input_string)
print("Reversed string:", reversed_string)


Output
==========
Enter a String : python
Original string: python
Reversed string: nohtyp







write a python program to reverse only the vowels of a given string

Explanation
===================
Ask the user to  input a string using the input function call  store the given string into a variable.  then take another  variable name as vowel_string and initialize all the vowels to it as a string  literal.

Use a for loop to iterate all the letters present in the input string and take out all the vowels present in the  given string.

Use the print function call to print all the vowels present in the string and also reverse the string containing vowels and print it.


Complete Python Program
=============================
input_string = input("Enter a String : ")
vowel_string = "aeiouAEIOU"
output_string = ""

for letter in input_string:
   if letter in vowel_string:
       output_string += letter

print("Vowels in string is:", output_string)
print("Reverse of Vowels in string is:", output_string[::-1])


Output
==========
Enter a String : reverse a string
Vowels in string is: eeeai
Reverse of Vowels in string is: iaeee





write a python program to reverse string in a given list of string values using lambda

Explanation
===================
Define a Lambda function named as reverse_string,  use the  string slicing method to return the reverse of a string.

Now ask user to enter a sample string using input() function call and store it into input_string variable

Call the Lambda function by passing the given input string as input element to the Lambda function,  store the reverse of a string into another variable.

Print both the input and output strings


Complete Python Program
=============================
reverse_string = lambda x: x[::-1]

input_string = input("Enter a String : ")
output_string = reverse_string(input_string)

print("Original string:", input_string)
print("Reversed string:", output_string)

Output
==========
Enter a String : this is complete string blog
Original string: this is complete string blog
Reversed string: golb gnirts etelpmoc si siht






write a python program to reverse the string without changing the space position

Explanation
===================
Ask the user to  enter a string and store the string into a variable,  reverse the given input string and convert it into a string  removing all the spaces.

Take an empty string variable name does output_string  for the purpose of appending the new string.
 
Now take an index variable and initialize it to zero, Take a for loop to iterate all the letters present in input_string.

If the letter is space, append space if the letter is not a space, keep appending each letter present in temp_string to output_string using index variable and increment the index.

Once the for loop completes print the input and output string.


Complete Python Program
=============================
input_string = input("Enter a String : ")
temp_string = "".join(input_string[::-1].split(" "))
output_string = ""

index = 0
for letter in input_string:
   if letter in " ":
       output_string += letter
   else:
       output_string += temp_string[index]
       index += 1


print("Original string:", input_string)
print("Reversed string:", output_string)


Output
==========
Enter a String : code with tj
Original string: code with tj
Reversed string: jtht iwed oc




Post a Comment

0 Comments