-2

Around two weeks ago, I started learning Python, and the last few days I've been working with while and for loops. I've learned how to print each string in a list on its own line, but how can I print each LETTER of each string on its own line, before moving to the next string?? Here's the short code for printing the string value of each index in my list:

fruits = ["apple", "banana", "tangerine"]

for strings in fruits: 
    print(fruits[0])


 Output: apple
         apple
         apple 


 Desired output: a
                 p 
                 p
                 l
                 e

                 b
                 a
                 n
                 a
                 n
                 a..... etc. 

Thanks in advance for whoever can help me out here. I'm still learning the logic and syntax of python, and I figured completely understanding loops was very necessary.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 1
    Try putting a loop within another loop: e.g. `for letters in strings`. Python will treat your string as an array. – Kris Mar 15 '21 at 18:52
  • 2
    When you do `for strings in fruits:`, how many times do you expect that to run? Why? What do you expect the value of `strings` to be the first time through the loop? Why? How about each other time - what is your understanding of how it works? Now - try to think of a way to use the value of `strings` to do what you need to do. Hint: what is the relationship between the value of `strings`, and the letters you want to print? – Karl Knechtel Mar 15 '21 at 18:53
  • Anyway, this question is off-topic for Stack Overflow. This is not a tutorial website. You should try following through an actual tutorial, or asking a question on an actual forum such as Reddit. – Karl Knechtel Mar 15 '21 at 18:54
  • Thank you both for the responses; Kris, you were helpful in confirming that I did indeed need a nested for loop, and Karl: I expected it to run 3 times, since the index length was 3. However, I was not sure if it was relevant in any way, or necessary. Thanks for your help, on making me realize I have made almost no progress. – SubZroh Mar 15 '21 at 20:56

5 Answers5

1

You want to iterate over each word as well as iterating over the list of words. Iterating over a string gives you one character of the string on each iteration:

fruits = ["apple", "banana", "tangerine"]

for fruit in fruits:
    for c in fruit:
        print(c)
    print()

Result:

a
p
p
l
e

b
a
n
a
n
a

t
a
n
g
e
r
i
n
e

The extra print() gives you the blank line between each word's characters.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Ahh, thank you so much!! I believe that the confusion was within the for loop names.. the 'fruit' and 'c' are the parts I kept messing with to no avail. Thank you, truly. – SubZroh Mar 15 '21 at 20:51
1
In [1]: fruits = ["apple", "banana", "tangerine"]                                                                                                                                                                                                                             

In [2]: for fruit in fruits: 
   ...:     print(fruit) 
   ...:                                                                                                                                                                                                                                                                       
apple
banana
tangerine

In [3]: for fruit in fruits: 
   ...:     for letter in fruit: 
   ...:         print(letter) 
   ...:                                                                                                                                                                                                                                                                       
a
p
p
l
e
b
a
n
a
n
a
t
a
n
g
e
r
i
n
e

In [4]: for fruit in fruits: 
   ...:     for letter in fruit: 
   ...:         print(letter) 
   ...:     print()  # this will cause an empty space between each fruit 
   ...:                                                                                                                                                                                                                                                                       
a
p
p
l
e

b
a
n
a
n
a

t
a
n
g
e
r
i
n
e


In [5]: for fruit in fruits: 
   ...:     print(*fruit, sep='\n') 
   ...:     print() 
   ...:                                                                                                                                                                                                                                                                       
a
p
p
l
e

b
a
n
a
n
a

t
a
n
g
e
r
i
n
e

Check out the print documentation to understand what sep does. Also check out this post to understand what * does

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

I added a return per each string:

fruits = ["apple", "banana", "tangerine"]

for string in fruits:
    for char in string:
        print(char)
    print("\n")
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
0

use .join

fruits = ["apple", "banana", "tangerine"]

for letter in " ".join(fruits):
    print(letter)

Output:

a
p
p
l
e

b
a
n
a
n
a

t
a
n
g
e
r
i
n
e
Coconutcake
  • 156
  • 9
0

The commenter Kris is correct. To elaborate slightly, I'll add this bit:

The for statement in Python will cause iteration over any object that defines an __iter__ default method. This includes sequences (list, tuple, dict, str) and generators (range, map, filter, any function which has a yield statement).

Also, any user-defined class can be looped through with for by defining an __iter__ method.

N. Kern
  • 401
  • 2
  • 7