Adding a string to a list is a common operation in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to append or insert a string into a list efficiently.
Example 1:
fruits = ['apple', 'banana', 'kiwi'] fruits.append('orange') print(fruits)
Output: [‘apple’, ‘banana’, ‘kiwi’, ‘orange’]
Example 2:
numbers = [1, 2, 3, 5] numbers.insert(3, 'four') print(numbers)
Output: [1, 2, 3, ‘four’, 5]