[python] Printing one character at a time from a string, using the while loop

Im reading "Core Python Programming 2nd Edition", They ask me to print a string, one character at a time using a "while" loop.

I know how the while loop works, but for some reason i can not come up with an idea how to do this. I've been looking around, and only see examples using for loops.

So what i have to do:

user gives input:

text = raw_input("Give some input:  ")

I know how to read out each piece of data from an array, but i can't remember anything how to do it to a string.

Now all i need is working while-loop, that prints every character of the string, one at a time.

i guess i've to use len(text), but i'm not 100% sure how to use it in this problem.

Some help would be awsome! I'm sure this is a very simple issue, but for some reason i cannot come up with it!

Thx in advance! :)

This question is related to python while-loop

The answer is


Strings can have for loops to:

for a in string:
    print a

This will print each character in text

text = raw_input("Give some input:  ")
for i in range(0,len(text)):
   print(text[i])

Try this procedure:

def procedure(input):
    a=0
    print input[a]
    ecs = input[a] #ecs stands for each character separately
    while ecs != input:
        a = a + 1
        print input[a]

In order to use it you have to know how to use procedures and although it works, it has an error in the end so you have to work that out too.


Other answers have already given you the code you need to iterate though a string using a while loop (or a for loop) but I thought it might be useful to explain the difference between the two types of loops.

while loops repeat some code until a certain condition is met. For example:

import random

sum = 0
while sum < 100:
    sum += random.randint(0,100) #add a random number between 0 and 100 to the sum
    print sum

This code will keep adding random numbers between 0 and 100 until the total is greater or equal to 100. The important point is that this loop could run exactly once (if the first random number is 100) or it could run forever (if it keeps selecting 0 as the random number). We can't predict how many times the loop will run until after it completes.

for loops are basically just while loops but we use them when we want a loop to run a preset number of times. Java for loops usually use some sort of a counter variable (below I use i), and generally makes the similarity between while and for loops much more explicit.

for (int i=0; i < 10; i++) { //starting from 0, until i is 10, adding 1 each iteration
    System.out.println(i);
}

This loop will run exactly 10 times. This is just a nicer way to write this:

int i = 0;
while (i < 10) { //until i is 10
   System.out.println(i);
   i++; //add one to i 
}

The most common usage for a for loop is to iterate though a list (or a string), which Python makes very easy:

for item in myList:
    print item

or

for character in myString:
    print character

However, you didn't want to use a for loop. In that case, you'll need to look at each character using its index. Like this:

print myString[0] #print the first character
print myString[len(myString) - 1] # print the last character.

Knowing that you can make a for loop using only a while loop and a counter and knowing that you can access individual characters by index, it should now be easy to access each character one at a time using a while loop.

HOWEVER in general you'd use a for loop in this situation because it's easier to read.


Python Code:

for s in myStr:
        print s

OR

for i in xrange(len(myStr)):
    print myStr[i]

   # make a list out of text - ['h','e','l','l','o']
   text = list('hello') 

   while text:
       print text.pop()

:)

In python empty object are evaluated as false. The .pop() removes and returns the last item on a list. And that's why it prints on reverse !

But can be fixed by using:

text.pop( 0 )

Python allows you to use a string as an iterator:

for character in 'string':
    print(character)

I'm guessing it's your job to figure out how to turn that into a while loop.


Try this instead ...

Printing each character using while loop

i=0
x="abc"
while i<len(x) :
    print(x[i],end=" ")
    print(i)
    i+=1