How do you reverse a string in python without function?

This is a Python Program to reverse a string without using recursion.

Problem Description

The program takes a string and reverses the string without using recursion.

Problem Solution

1. Take a string from the user.
2. Use string slicing to reverse the string.
3. Print the reversed string.
4. Exit.

Program/Source Code

Here is source code of the Python Program to reverse a string without using recursion. The program output is also shown below.

a=str(input("Enter a string: "))
print("Reverse of the string is: ")
print(a[::-1])

Program Explanation

1. User must enter a string.
2. By giving the increment value as -1, string slicing is used to reverse the list.
3. The reversed string is printed.

Runtime Test Cases

 
Case 1:
Enter a string: hello
Reverse of the string is: 
olleh
 
Case 2:
Enter a string: test
Reverse of the string is: 
tset

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

How do you reverse a string in python without function?

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Educative Answers Team

In Python, strings are ordered sequences of character data.

There is no built-in method to reverse a string. However, strings can be reversed in several different ways.

Methods

Three methods to reverse a string are explained below:

1. Slicing

Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0.

To reverse a string using slicing, write:

stringname[stringlength::-1] # method 1 

Or write without specifying the length of the string:

stringname[::-1] # method2

The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).

s="Python" # initial string

stringlength=len(s) # calculate length of the list

slicedString=s[stringlength::-1] # slicing

print (slicedString) # print the reversed string

2. Loop

To start, let’s create a new array called reversedString[].

We can then loop over the list with iterating variable index initialized with the length of the list.

  • In each iteration, concatenate value of str[index-1] with reverseString
  • Decrement the index.

We then simply keep iterating until the index is less than zero.

s = "Python" # initial string

reversedString=[]

index = len(s) # calculate length of string and save in index

while index > 0:

reversedString += s[ index - 1 ] # save the value of str[index-1] in reverseString

index = index - 1 # decrement index

print(reversedString) # reversed string

3. Use join

This is a powerful technique that takes advantage of Python’s iterator protocol. This technique reverses a string using reverse iteration with the reversed() built-in function to cycle through the elements in the string in reverse order and then use .join() method to merge all of the characters resulting from the reversed iteration into a new string.

The general syntax is

s="Python" 
reversedstring=''.join(reversed(s))

The following Python code demonstrates the concept.

s = 'Python' #initial string

reversed=''.join(reversed(s)) # .join() method merges all of the characters resulting from the reversed iteration into a new string

print(reversed) #print the reversed string

Copyright ©2022 Educative, Inc. All rights reserved

How do I reverse a string in Python manually?

Some of the common ways to reverse a string are:.
Using Slicing to create a reverse copy of the string..
Using for loop and appending characters in reverse order..
Using while loop to iterate string characters in reverse order and append them..
Using string join() function with reversed() iterator..

How do you reverse a string without using the reverse method?

C program to reverse a string without using string function(strrev).
Logic. We start our for loop from the end of the string and keep printing each character till we reach the first character..
Dry Run of the Program. Take input string 'str'.Let us take str=”code” ... .
Program. ... .
Output..

How many ways can you reverse a string in Python?

In this article, you will learn 5 different ways to reverse the string in Python..
1) Using for Loop. ... .
2) Using while Loop. ... .
3) Using Slicing. ... .
4) Using join() and reversed() Methods. ... .
5) Using List reverse().