16 Must Know Array of Strings Function in Python

In Python, we don’t have a pre-defined feature for character data types, as there every single character in python is treated as a string by itself. An array of strings can be defined as the capacity of a string to contain more than one string value at the same time. we can call them and access them at any time in the program during the execution process. In this tutorial, we will be discussing various types of arrays of strings in python.

List

The list is very useful for storing multiple values in a single variable. The main feature of the list is that it allows duplicates. If we want to frequent changes in our data list are mostly used for this purpose. Let us look at the example of a list:

#example of list
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
print(names)

Output:

['siddharth', 'khushi', 'shivani', 'sandeep']

Explanation:

  • Firstly, we will make a list of names.
  • The list is enclosed inside [ ] square brackets.
  • At last, we will print the list that can be said as an array of strings.

Different Functions to Operate array of strings in Python

Here, we will be discussing all the functions of an array of strings in python with the examples explained in detail. So that you can understand the concept clearly.

1. Accessing array of strings in Python by index

In this example, we will try to access the list through its index value. The Positive index value starts from the start of the list and from 0 index value as the first index value. Let us look at the example for understanding the concept in detail.

#example of list indexing
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
print(names[1])

Output:

khushi

Explanation:

  • Firstly, we will make a list of names.
  • Then, we will try to access the item of list through its index value
  • At last, we will print the output by accessing the name of someone with an index.

2. Accessing array of strings in Python by negative index

In this example, we will try to access the list through its negative index value. The negative index value starts from the last of the list and the -1 index as the first index value. Let us look at the example for understanding the concept in detail.

#example of list negative indexing
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
print(names[-1])

Output:

sandeep

Explanation:

  • Firstly, we will make a list of names.
  • Then, we will try to access the item of list through its negative index value
  • At last, we will print the output by accessing the name of someone with a negative index.

3. Len of array of strings in Python

In this example, we will calculate the length of the string with the help of the len() function in python. Let us look at the example for understanding the concept in detail.

#example of len of a list
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
print(len(names))

Output:

4

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will calculate the length of the list with the help of the len() function in python.
  • At last, we will print the output.

4. Append values in the array of strings in Python

In this example, we will append the values in the list or you can say we will add the value in the list. With the help of the append() function, you can add the values to the list. Let us look at the example for understanding the concept in detail.

#example of adding values in list 
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
names.append('upasna')
print(names)

Output:

['siddharth', 'khushi', 'shivani', 'sandeep', 'upasna']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the append() function in the list names.
  • Inside the append() function, we will pass the name which we want to append.
  • At last, we have printed the output.

5. Looping in the array of strings in Python

In this example, we will be applying a loop for accessing the list. Let us look at the example for understanding the concept in detail.

#looping in list
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
for i in names:
    print(i)

Output:

siddharth
khushi
shivani
sandeep

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply for loop.
  • And at each iteration, we will print the value of the list item.
  • At last, you can see the output.

6. Removing values from array of strings in Python

In this example, we will be removing the values from the array of strings or lists with the help of the pop() function. The pop() function is used to remove the element from the list by its index value. And remove() function is used to remove the particular value from the list. Let us look at the example for understanding the concept in detail.

#example of removing values in list 
names = ['siddharth', 'khushi', 'shivani', 'sandeep', 'upasna']
names.pop(4)
print(names)

names.remove('shivani')
print(names)

Output:

['siddharth', 'khushi', 'shivani', 'sandeep']
['siddharth', 'khushi', 'sandeep']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will try to pop the value through its index number by applying the pop() function.
  • After that, we will print the entire list after popping the element.
  • And then, we will remove the particular item from the list by applying the remove() function.
  • At last, we will print the list.
  • Hence, you can see both outputs.

7. Clear() function in an array of strings in python

In this example, we will discuss the clear() function in the arrays of strings. The clear () function is used to clear or empty the list. Let us look at the example for understanding the concept in detail.

#example of clear list
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
names.clear()
print(names)

Output:

[]

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the clear() function which clears the entire list.
  • At last, we will print the output.
  • Hence, you can see the entire list is now empty.

8. copy() function in an array of strings in python

In this example, we will copy the entire list to another list with the help of the copy() function. Copy() function is used to copy the list into another list. Let us look at the example for understanding the concept in detail.

#example of copy list
names = ['siddharth', 'khushi', 'shivani', 'sandeep']
subject = names.copy()
print(subject)

Output:

['siddharth', 'khushi', 'shivani', 'sandeep']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the copy() function which will copy the entire list into another list.
  • The entire list is copied to the new list named subject.
  • At last, we will print the output.
  • Hence, you can see the subject list contains the same element as the names list.

9. count() function

In this example, we will be using the count() function which is used to calculate the count of the particular element present in the list. Let us look at the example for understanding the concept in detail.

#example of count list
names = ['siddharth', 'khushi', 'shivani', 'sandeep', 'khushi']
subject = names.count('khushi')
print(subject)

Output:

2

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the count() function inside which we have passed a particular name whose count we need to find.
  • At last, we have printed the output and the count occurs of the name = ‘Khushi.
  • Hence, you see the count value as the output.

10. extend() function

In this example, we will be using extend() function which is used to add the second list in one list at the end of the first list. Let us look at the example for understanding the concept in detail.

#example of extend list
names = ['orange', 'blue', 'red']
colors = ['pink', 'black', 'white']

names.extend(colors)
print(names)

Output:

['orange', 'blue', 'red', 'pink', 'black', 'white']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will take another list named colors.
  • After that, we will extend the second list to the first list with the help of extend() function.
  • At last, we have printed the output.
  • Hence, you can see both the list are merged in one list.

11. insert() function

In this example, we will be using the insert() function which is used to insert an item at a particular position in the list. Let us look at the example for understanding the concept in detail.

#example of insert list
names = ['orange', 'blue', 'red']

names.insert(1,'black')
print(names)

Output:

['orange', 'black', 'blue', 'red']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the insert() function.
  • Inside which we have passed two parameters. first is the index at which you want to insert and the second is which item you want to insert.
  • At last, we have printed the output.
  • Hence, you can see the inserted element at the particular position.

12. reverse() function

In this example, we will be using the reverse() function which is used to reverse the entire list of elements. Let us look at the example for understanding the concept in detail.

#example of reverse list
names = ['orange','black', 'blue', 'red']

names.reverse()
print(names)

Output:

['red', 'blue', 'black', 'orange']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the reverse() function.
  • At last, we will print the list.
  • Hence, you can see the list gets printed in reverse order.

13. sort() function

In this example, we will be using the sort() function which is used to sort the elements of a list in ascending order. we can also do the reverse order sorting in the list. Let us look at the example for understanding the concept in detail.

#example of sort list
names = ['orange','black', 'blue', 'red']

names.sort()
print(names)
print("\n")
names.sort(reverse = True)
print(names)

Output:

['black', 'blue', 'orange', 'red']


['red', 'orange', 'blue', 'black']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply the sort() function in the list.
  • After that, we will print the output and see the list gets sorted in ascending order.
  • Then, we will again apply the sort() function and inside it, we will set parameter reverse = True. so that the list gets sorted in descending order.
  • At last, we will print the output.
  • Hence, you can see both the output in the sorted order.

14. range of indexes

In this example, we will be printing the list item from a particular range to a particular range. Let us look at the example for understanding the concept in detail.

#example of range of indexes list
names = ['orange','black', 'blue', 'red','pink']

print(names[1:4])

Output:

['black', 'blue', 'red']

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will set the particular range of index and print the output.
  • At last, you will see the output from the range given.

15. change of item value

In this example, we will be changing the value of the particular item to something else according to our wish. Let us look at the example for understanding the concept in detail.

#example of change of item value list
names = ['orange','black', 'blue', 'red','pink']
names[1] = 'white'
print(names)

Output:

['orange', 'white', 'blue', 'red', 'pink']

Explanation:

  • Firstly, we will create a list of names.
  • Them, we will set the particular index with the other item in the list.
  • At last, we will print the output.
  • hence, you can see the output in which the particular index value gets changed with the new value.

16. checking existence of an element

In this example, we will be checking if the particular element is present in the list or not. Let us look at the example for understanding the concept in detail.

#example of checking existence in list
names = ['orange','black', 'blue', 'red','pink']
if 'blue' in names:
    print("present")
else:
    print("not present")

Output:

present

Explanation:

  • Firstly, we will create a list of names.
  • Then, we will apply if condition in which we will check the particular element is present inside the list or not.
  • If the element is present in the list, it will print present else not present.
  • Hence, you can see the output.

Conclusion

In this tutorial, we have discussed the concept of arrays of strings. We have discussed that arrays of a string are the same as the list in python. we have also discussed all the functions that can be performed on the list with the examples explained in detail. You can use any of the functions according to your choice and as per your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments