What is Reversing a String?
Reversing a string means reversing the order of characters in a given string.
For example, if we reverse the string "Hello", we get "olleH"
Pseudo Logic
- Create a method named reverse_string(input_string) that takes in a input_string argument.
- Initialize an empty String variable say reversed_string.
- Iterate through each character using a for loop of the input string in reverse order.
- Now append each character to the reversed_string variable.
- Return the reversed_string variable.
To reverse a string in Python, follow these steps to build your logic:
Python Reverse String Program
#
# Program 13: String Reversal
# 1000+ Python Programs by Code2care.org
#
def reverse_string(input_string):
"""
Args:
input_string (str): The string to be reversed.
Returns:
str: The reversed string.
"""
reversed_string = ''
# Iterate through each character of the input string in reverse order
for i in range(len(input_string) - 1, -1, -1):
# Append each character to the reversed_string variable
reversed_string += input_string[i]
# Return the reversed string
return reversed_string
# Let's try a few examples
print(reverse_string("John"))
print(reverse_string("Stephen"))
print(reverse_string("Andrew"))
print(reverse_string("Harry"))
Output:
nhoJ
nehpetS
werdnA
yrraH
String Reversal using Python Built-in Method
The built-in reversed() function that can be used to reverse a String.
input_str = "Voldomort"
reversed_str = ''.join(reversed(input_str))
print(reversed_str)
Output:
tromodloV
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- Step-by-Step: How to delete a git branch from local as well as remote origin - Git
- How to Gzip a file directory on Mac OS X using Terminal Command - Mac-OS-X
- bash: netstat: command not found - Bash
- Python: Sort List in Descending Order - Python
- Formatting Double in Java [Examples] - Java
- How to Install MongoDB in VSCode - HowTos
- How to check if a String contains substring or a word using javaScript - JavaScript
- Java 8 Format Date and Time Examples - Java