Skip to main content
python-array-of-strings

Python Array of Strings

This tutorial helps to create python array of strings. Python does not have built-in support for Arrays. Python lists are used to create an array using capacity. An array is a collection of elements of the same type.

In Python every single character in python is treated as a string by itself. A single character in itself is a string with length 1. We can create an array of strings in python using the list.

You can also checkout other recommended python tutorials:

There are a number of pre-defined methods available for common uses and operations. You can access list items by index, looping into the python array, appending an item into a list, the length using len() method, removing using pop() method, extend() the list, etc.

I’ll cover all the above scenarios in this tutorial step by step.

  • Differences Between Python Lists and Arrays
  • Creating an Array of Strings
  • Accessing Array Elements in Python
  • Slicing Arrays in Python
  • Using Negative Indexing with Arrays
  • Getting the Length of an Array in Python
  • Iterating Through Python Arrays
  • Adding Elements to an Array in Python
  • Removing Elements from an Array in Python
  • Removing Elements by Value in Python Arrays
  • Extending Python Arrays

Python Lists Vs array

You can use the list as an array. The main difference is the type of element stored in the list. The array will store the same type of element whereas the list can store the different types of elements.

//list
a = [1, 2, 4.5, "pythonpip"] 
//array
import array as arr
a = arr.array(1, 2, 4.5, "pythonpip")   // Error

How To Create an Array of String

We need to import an array module to create arrays:

a = arr.array('i', [1, 3, 4])
print(a)

We created an array of integer types. The letter 'i' is a type code of array that will store elements. This determines the type of the array during creation.

CodeC TypePython TypeMin bytes
'b'signed charint1
'B'unsigned charint1
'u'Py_UNICODEUnicode2
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intint2
'l'signed longint4
'L'unsigned longint4
'f'floatfloat4
'd'doublefloat8

How to access array elements?

You can access array elements using the array index. The array element index start with 0 as like python list.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
print("First element:", a[0])
print("Second element:", a[1])
print("Last element:", a[-1])

Output:

First element: 1
Second element: 3
Last element: 8

How to slice arrays?

Slicing is a common operation that allows you to extract a portion of a sequence from an array. We can slice by using the slicing operator : .

import array as arr
a = arr.array('i', [1, 3, 4, 8, 12, 23])
print("2nd to 4th element:", a[1:4])
print("Beginning to 3rd element:", a[:-3])
print("4th to end element:", a[3:])

Output:

 $python main.py
('2nd to 4th element:', array('i', [3, 4, 8]))
('Beginning to 3rd element:', array('i', [1, 3, 4]))
('4th to end element:', array('i', [8, 12, 23]))

Negative Indexing

You can also access the array element using the negative index. The last element can access using the -1 index, the second last will be -2, so on.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
print("First element:", a[-1])
print("Second element:", a[-2])

Output:

First element: 8
Second element: 4

How To Get Array Length

We can get the array length using len() method. The len() function to is used to the length of arrays, lists, tuples, dictionaries (to get the number of key-value pairs), strings (to get the number of characters) in Python.

import array as arr<br>
a = arr.array('i', [1, 3, 4, 8])<br>
print("Length:", len(a))

Output:

Length: 4

How to Iterate on Python Array

We can iterate on a python array using for loop. The for loop in Python is a popular way to loop through iterable objects like array. Here’s how you can use it:

import array as arr
a = arr.array('i', [1, 3, 4, 8])

for x in a
  print(x)

Output:

1
3
4
8

How to add an Element into an Array

We can also add an element into an array using append() method. The append() method allows you to add a single element to the end of the list.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
a.append(12)
print(a)

Output:

array('i', [1, 3, 4, 8, 12])

How To Remove an Element from an Array

Removal of any element can be done via the pop() method. We can delete any particular element by referring to the index.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
a.pop(3)
print(a)

Output:

array('i', [1, 3, 4])

The pop(1) will remove the second element from the list.

How to Remove Element By value

We can also use the remove() method to delete the element from an array. The remove method takes the element value as a parameter that you want to remove.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
a.remove(3)
print(a)

Output:

array('i', [1, 4, 8])

How To Extend the Array

We can add the elements to the end of the list using extend() method. This method appends all the elements from the a source list to the end of the existing list.

import array as arr
a = arr.array('i', [1, 3, 4, 8])
b = arr.array('i', [7, 2, 5])
a.extend(b)
print(a)

This is a useful way to add multiple elements to an existing list without having to append them one by one. The original list is modified in place.

Output:

array('i', [1, 3, 4, 8, 7, 2, 5])

The list has append() and extends() method to concatenate or join two arrays. You can also do the same action by ‘+’ operator.

2 thoughts to “Python Array of Strings”

Leave a Reply

Your email address will not be published. Required fields are marked *