for loop to create list of strings in python

To create a list of strings using a for loop in Python, you can follow these steps:

  1. Create an empty list to store the strings.
  2. Create a for loop that iterates as many times as the number of strings you want to create.
  3. Inside the for loop, use string concatenation to create a new string.
  4. Append the new string to the list created in step 1.

Here's an example code snippet that creates a list of strings with the values "string1", "string2", ..., "string5":

main.py
string_list = []
for i in range(1, 6):
    new_string = "string" + str(i)
    string_list.append(new_string)
print(string_list)
128 chars
6 lines

Output:

main.py
['string1', 'string2', 'string3', 'string4', 'string5']
56 chars
2 lines

In this example, we used the range() function to create a sequence of numbers from 1 to 5, and we used the str() function to convert the integer i into a string so that we can concatenate it with the prefix "string". The append() method was used to add each new string to the list.

gistlibby LogSnag