• Post author:
  • Post category:Python
  • Post last modified:March 27, 2024
  • Reading time:9 mins read
You are currently viewing How to Create Array of Strings in Python

How to create an array of strings in Python? you can create an array of strings using the list. Python does not have a built-in data Type. You can represent the list as an array. An array is a collection of elements of the same data type. In Python, every single character is represented as a string. You can create and implement the array of strings using various operations and methods of list.

In this article, I will explain how to create and implement an array of strings using lists with multiple examples.

1. Quick Examples of Creating an Array of Strings

Following are quick examples of creating an array of strings.


# Below are some quick examples

# Example 1: Create array of strings using list
# Create an empty Array
arr_str = [] 
arr_str.append("Spark")
arr_str.append("by")
arr_str.append("examples")

# Example 2: Access specified array string
print("Specified string:", arr_str[0])

# Example 3: Access multiple strings using slicing
print(arr_str[:2])

# Example 4: Iterate array of stings using for loop 
# get the strings
for str in arr_str:
    print(str)

# Example 5: Iterate array of stings using while loop 
# get the strings
i = 0
while i < len(arr_str):
    print(arr_str[i])
    i += 1

# Example 6: Add elements to an array
arr_str[1] = 'PySpark' 
arr_str[2] = 'Python'

# Example 7: Append the new string to existing array of strings
arr_str.append('PySpark')

# Example 8: Remove specified string from an array
arr_str.remove('examples')

# Example 9: Pop string from an array
arr_str.pop(1)

2. Create an Array of Strings using Python List

There is no built-in array data structure in Python specifically for storing strings. However, you can use a list to create an array of strings. First, create an empty array and then add stings to the array using the append() function. Finally, you can get an array of strings.


# Create array of strings using list
# Create an empty Array
arr_str = [] 
print("Empty array:", arr_str)
arr_str.append("Spark")
arr_str.append("by")
arr_str.append("examples")
print("Array of strings:", arr_str)
Python array string

3. Access Specified String in Python Array

You can access specified strings of an array using their index. Arrays are a 0-based index, hence you can use [](bracket) notation to access a particular string based on its corresponding index.


# Access specified array string
print("Array of strings:", arr_str)
print("Specified string:", arr_str[0])
Python array string

4. Access Multiple Strings using Slicing

Moreover, you can access multiple strings from an array of strings using the slicing operator. Let’s apply the slicing operator over the given array of strings and get the specified selection of an array. For example,


# Access multiple strings using slicing
print("Array of strings:", arr_str)
print("Access multiple strings:", arr_str[:2])

# Output:
# Array of strings: ['Spark', 'by', 'examples']
# Access multiple strings: ['Spark', 'by']

5. Iterate Over the Python Array and get the Strings

You can iterate arrays using for loop to access each string present in the array. Use For loop in Python over the sequence or iterable objects such as lists, sets, strings, tuples, and dictionaries to perform various operations in Python.


# Iterate array of stings using for loop 
# get the strings
for str in arr_str:
    print(str)

# Output:
# Spark
# by
# examples

Alternatively, you can use a while loop to access each string present in the array. Using a while loop you can iterate a string as long as the condition is True. If the condition is True then print the element of the array, if the condition is False loop will come out.


# Iterate array of stings using while loop 
# get the strings
i = 0
while i < len(arr_str):
    print(arr_str[i])
    i += 1

# Output:
# Spark
# by
# examples

6. Add Strings to Python Array

You can add strings to an array by using its index. Using the index [] operator you can add/change the elements of an array. For example,


# Add elements to an array
print("Array of strings:", arr_str)
arr_str[1] = 'PySpark' 
arr_str[2] = 'Python'
print("After changing the array of strings:", arr_str)


# Output:
# Array of strings: ['Spark', 'by', 'examples']
# After changing the array of strings: ['Spark', 'PySpark', 'Python']

You can also use the insert() method to add an element at a specific index of the array. For example, you use the insert() method to add the string(PySpark) at the index 0 of the array. The existing elements are shifted to the right to make room for the new element.


# Add elements to array using insert()
print("Array of strings:", arr_str)
arr_str.insert(0, "PySpark")
print(" After inserting array is:", arr_str)

# Output
# Array of strings: ['Spark', 'by', 'examples']
# After inserting array is: ['PySpark', 'Spark', 'by', 'examples']

You can also use extend() method to add elements to an array of existing by appending all the elements of another iterable (such as a list, tuple, or another array) to the end of the array.


# Extend the array using another array
print("Array1:", arr_str)
arr_str1 = ["is", "a", "good", "page"]
print("Array2:", arr_str1)
arr_str.extend(arr_str1)
print(" After extending the array is:", arr_str)

# Output
# Array1: ['Spark', 'by', 'examples']
# Array2: ['is', 'a', 'good', 'page']
# After extending the array is: ['Spark', 'by', 'examples', 'is', 'a', 'good', 'page']

7. Append String to Python Array

You can use the append() method to append the new string to the end of the existing array of strings.


# Append the new string to existing array of strings
print("Array1:", arr_str)
arr_str.append('PySpark')
print(" After extending the array is:", arr_str)

# Output
# Array1: ['Spark', 'by', 'examples']
# After extending the array is: ['Spark', 'by', 'examples', 'PySpark']

8. Remove Strings from Python Array

In Python, use the built-in remove() function to remove strings from an array of strings. To pass the specified string(which we want to remove) into remove(), it will remove those elements from the array. If the passed element doesn’t exist in the array, it can raise the ValueError.


# Remove specified string from an array
print("Array of strings:", arr_str)
arr_str.remove('examples')
print(" After removing the array is:", arr_str)


# Output:
# Array of strings: ['Spark', 'by', 'examples']
# After removing the array is: ['Spark', 'by']

pop() function can also be used to remove the element of an array and return the removed element. By default, it removes only the last element of the array. If we remove a specified element you can pass its corresponding index into the pop() function.


# Pop string from an array
print("Array of strings:", arr_str)
arr_str.pop(1)
print(" After the pop an array is:", arr_str)

# Output:
# Array of strings: ['Spark', 'by', 'examples']
#  After the pop an array is: ['Spark', 'examples']

9. Conclusion

In this article, I have explained how to create an array of strings using Python list and also explained using some of the operations and methods of list how we can implement the array of strings with examples.

Happy Learning!!

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.