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 :
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 :
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 :
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 :
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:
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 :
No comments:
Post a Comment