Wednesday, 7 August 2019

Python 3 String Questions with Answers

Python 3 String Questions with Answers

How do you reverse a input string in python 3?

Question 1: Write a program to reverse a string in python

Answer :

name=input("Enter any name")
print("The given name is : ",name)
length=len(name)
print("The reversed name is : ")
for i in range(-1,(-length-1),-1) :
        print(name[i])


Screenshot :


Reverse a Input string in python using for loop



Question 2 : Write a program to reverse a string using function?


Answer :


def reverse(str) :
       s=""
for ch in str :
       s=s+ch
return s    
mystr(input("Enter any string "))
print("The given string is : ",mystr)
print("The Reversed string is : ",reverse(mystr))


Screenshot :


Reverse a string using function in python




Index of character in a string in python 


Question 3 : Write a loop that prints out the index of every 'i' in 'Mississippi' ?

Answer :

string="Mississippi"
index=0
ch='i'
for i in string :
      if(ch==i) :
              print("The index number of i is : ",index)
       index=index+1


Screenshot : 


display index number of given character in a string using python




Count a number 


Question 4 : Write a program to count the execution of program using for loop ?

Answer :

count=0
for i in range(10,100,5) :
         if(i%4==0) :
                  count=count+1
print(count)


Screenshot:


count number of execution of program in python




Print Table in Python 


Question 5 : Write a program to take an integer input N from the user .Print the table of N ?

Answer :

num=int(input("Enter any number"))
for i in range(num,num*11,num) :
            print(i)


Screenshot :
display table of any number in python







    

No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...