Python Sorting Strings

We can sort a group of strings into alphabetical order using sorted() function. The sort() method is used in the following way:

str.sort()

Here, 'str' represents an array that contains a group of strings. When sort() method is used, it will sort the original array, i.e. 'str'. So, the original order of strings will be lost and we will have only one sorted array. To retain the original array even after sorting, we can use sorted() function as:

str1 = sorted(str)

A Python program to sort a group of strings into alphabetical order.


Searching in the Strings

The easiest way to search for a string in a group of 'n' strings is by using sequential search or linear search technique. This is done by comparing the searching string 's' with every string in the group. For this purpose, we can use a for loop that iterates from 0th to n-1th string in the group. By comparing the searching string 's' with every one of these strings, we can decide whether 's' is available in the group or not. The logic looks something like this: