[Solved] Loop with characters and integers


For generating a list from 1 to n, we use range

In [8]: n = 10                                                                                                                 

In [9]: for i in range(1,n+1): 
   ...:     print(i) 
   ...:                                                                                                                        
1
2
3
4
5
6
7
8
9
10

Building on this, to generate the string you want from 1 to n, we do as follows, we build the string using string.format

In [10]: for i in range(1,n+1): 
    ...:     print('Iteration {}'.format(i)) 
    ...:                                                                                                                       
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
Iteration 10

Combining the ideas above, The last thing needed is to append all these in a list using list.append

In [11]: li = []                                                                                                               

In [12]: for i in range(1,n+1): 
    ...:     li.append('Iteration {}'.format(i)) 
    ...:                                                                                                                       

In [13]: li                                                                                                                    
Out[13]: 
['Iteration 1',
 'Iteration 2',
 'Iteration 3',
 'Iteration 4',
 'Iteration 5',
 'Iteration 6',
 'Iteration 7',
 'Iteration 8',
 'Iteration 9',
 'Iteration 10']

This is how I would think of solving the problems in steps, although you can do all this is a single-line of list-comprehension

In [14]: li = ['Iteration {}'.format(i) for i in range(1,11)]                                                                  

In [15]: li                                                                                                                    
Out[15]: 
['Iteration 1',
 'Iteration 2',
 'Iteration 3',
 'Iteration 4',
 'Iteration 5',
 'Iteration 6',
 'Iteration 7',
 'Iteration 8',
 'Iteration 9',
 'Iteration 10']

2

solved Loop with characters and integers