add string to array python code example
Example 1: python add element to array
my_list = []
my_list.append(12)
Example 2: python add a string to a list
list = ["other str", 2, 56]
list.append("string")
# list = ["other str", 2, 56, "string"]
Example 3: python add string and number in array
numbers = []
animals = []
with open('textfile.txt') as f:
for x in f:
try:
numbers.append(int(x.replace("\n", "")))
except:
animals.append(str(x.replace("\n", "")))
print(numbers)
print(animals)
# Output
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# ['dog', 'cat', 'fish', 'bird']