Program to reverse a string in Python

Program to reverse a string in Python. This Program contains function definition, its uses string initialization and print statement. This is for class 11,12, Beginners and B Tech computer science students.

Program to reverse a string

String means a group of characters . Character means like a to z and simple punctuation symbols that is required by a sentence . Here we have given a sentence as a string and in reverse we are getting the right solution even it is reversing space punctuation.

Source code :

i=0
#  Reverse a string    
# using  slice syntax   
# reverse(str) Function to reverse a string   
def reverse(str):   
    str = str[::-1]   
    return str   
    
s = "Archit is a good boy"  
#print(i)
print ("The original string  is : ",s)   
print ("The reversed string using extended slice operator  is : ",reverse(s)) 

Source code Explanation:

Aim: Reverse a string

i=0 #test variable
step 1: using slice syntax
step 2:reverse(str) Function to reverse a string
def reverse(str):
str = str[::-1]
return str
s = “csstudy”
print (“The original string is : “,s)
#print(i)
print (“The reversed string using extended slice operator is : “,reverse(s))

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/csstudy/source code file 2/program to reverse string in python.py 
The original string  is :  Archit is a good boy
The reversed string using extended slice operator  is :  yob doog a si tihcrA
>>> 

Output explanation:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

RESTART: C:/csstudy/source code file 2/program to reverse string in python.py
Step 1: Enter the original string
The original string is : Archit is a good boy
Step 2:Reverses and print the statement
The reversed string using extended slice operator is : yob doog a si tihcrA

Conclusion:

The program is running fine.

Thank you for reading, learning and copying the code of Program to reverse a string in Python. This Program contains function definition, its use, string initialization and print statement. This is for class 11,12, Beginners and B Tech computer science students. for more program like this, Practical file of class 11,12 and python project Students click below:
/category/python-programs/
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study

Leave a Comment

Your email address will not be published.