Q. Write a Python script that traverses through an input string and prints its characters in different lines- two characters per line.


You can understand by Watching video :-



Answer -

a = input("Enter a string = ")
for i in range (0 , len(a)+1 , 2 ) :
     print( a [ i - 2 : i])

Output :-

Enter a string = Path

Pa
th

>>>

Enter a string = Portal

Po
rt
al

>>>

5 Comments

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

  1. Its answer is wrong
    It would be

    a = input("Enter a string = ")
    for i in range (2 , len(a)+2 , 2 ) :
    print( a [ i - 2 : i])

    or

    a = input("Enter a string = ")
    for i in range (0 , len(a), 2 ) :
    print( a [ i : i+2])

    ReplyDelete
    Replies
    1. a = input("Enter a string = ")
      for i in range (2 , len(a)+2 , 2 ) :
      print( a [ i - 2 : i])

      or

      a = input("Enter a string = ")
      for i in range (0 , len(a), 2 ) :
      print( a [ i : i+2])

      Delete
    2. Our program is right. Please run that answer in IDLE Python.

      Delete
  2. Code= "powerful"
    For ch in code :
    Print (ch, '~', end=' ')

    ReplyDelete

Post a Comment

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

Previous Post Next Post