Q. Write a Python program that should perform the following four tasks:

(i) After getting a word (of input) from the user, your program should use a while (or for) loop to print out each of the letters of the word. Just remember that strings in Python start with element 0.

(ii) Your program should then use another loop to print out each of the letters of the (same) word in reverse order.

(ii) Make a new variable that is the original word in reverse and print that variable. You can do this very easily in Python with a "string slice".

(iv) Ask the user for a letter to count. Use another loop to count how many times that letter appears in the original word. Print out this count.


Answer :- 

(i)

str = input ("Enter the Word :- ")
for i in range (len(str)) :
    print (str[i])

(ii)

i = len(str) - 1
while ( i > -1):
    print( str[i] )
    i -= 1

(iii)

newstr = str[-1 : : -1]
print (newstr)

(iv)

letter = input ("Enter a letter to count :- ") 
count = 0
for i in str :
    if i == letter :
        count += 1
print ("Number of letter :- ", count )

Output :-

Enter the Word :- PathWalla
P
a
t
h
W
a
l
l
a

a
l
l
a
W
h
t
a
P

allaWhtaP

Enter a letter to count :- a
Number of letter :-  3

>>>

8 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. but we have to use another loop in second part

    ReplyDelete
  2. but you used 'for' loop in both i and ii part

    ReplyDelete
  3. i part is giving output 'please enter again'

    ReplyDelete
  4. str = input ("Enter the Word :- ")
    a=0
    while a<len(str):
    print(str[a])
    a+=1
    We can use this coding in part 1 so that the program will print the each element of the string

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post