Lists in Python | Strings in Python – Complete Guide

In this article we will look at the list basics, appending elements to lists, removing list elements, sorting list elements, joining lists and various other fundamental list operations. We will also have a look at strings and the various operations that can be performed on them in Python.

Lists in Python

In the previous article we discussed the basics of lists and some of the functions that can be utilized on lists/list elements. Now we will have a look at their usage.

append (): is probably the most used list method. It is used to add items to end of the list.

 p_datatypes = ["Python", "C++"]
 p_datatypes.append("BMW")
 print(p_datatypes)
Output:
["Python", "апельсин", "BMW"]

insert (): insert() function is a very useful method for lists. It is used to insert an item into the list by index. Let’s look at an example insert()takes two arguments: the index where the new element should be inserted, and the element itself.

 p_datatypes = ["Python", "C++]
 p_datatypes.insert(1, “BMW”)
 print(p_datatypes)
Output:
["Python", "BMW", "C++"]
index (): index()helps to determine the index of an element. The following is an example of getting the index of a specific element.
lst = [1, 33, 5, 55, 1001]
 a = lst.index(55)
 print(a)
Output:
3
.clear (): The method .clear()removes all elements of the list.
 lucky_numbers = [5, 55, 4, 3, 101, 42]
 lucky_numbers.clear()
 print(lucky_numbers)
Output:
[]

.remove(): The method .remove()removes a specific item from the list.

 lucky_numbers = [5, 55, 4, 3, 101, 42]
 lucky_numbers.remove(101)
print(lucky_numbers)
Output:
[5, 55, 4, 3, 42]
reverse ():The method .reverse()expands the order of the items in the list.
lucky_numbers = [5, 55, 4, 3, 101, 42]
 lucky_numbers.reverse()
 print(lucky_numbers)
Output:
[42, 101, 3, 4, 55, 5]
.count ():The method is .count()used to count how often a particular item appears in the list. In the following example, we count how often the number 5 appears in the list. The result is 1, which means that the number 5 appears only once.
 lucky_numbers = [5, 55, 4, 3, 101, 42]
 print(lucky_numbers.count(5))
Output:
1
sum ():The function sum()will return the total sum of all numbers in the list.
lucky_numbers = [5, 55, 4, 3, 101, 42]
 print(sum(lucky_numbers))
Output:
210
min ():The function min()will show the element with the minimum value in the list. Let’s see an example.
 lucky_numbers = [5, 55, 4, 3, 101, 42]
 print(min(lucky_numbers))
Output:
3
 max ():The function max()will show the item with the highest value in the list. Example:
lucky_numbers = [5, 55, 4, 3, 101, 42]
print(max(lucky_numbers))
Output:
101

Strings in Python

String processing is an integral part of python programming. It is extremely rare for an application to not use string data types. Python provides a large collection of operators, functions, and methods for working with strings. Indexing/Slicing strings work according to the same logic as they do in tuples. Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets []. Strings can be created by placing characters within the single quote or double-quotes. Let us look at some built-in string functions:

len(): Returns the length of a string.

s = 'Python'
print(len(s))
Output:
6

in keyword: To check if a certain phrase or character is present in a string, we can use thein keyword.

txt = "The best things in life are free!"
print("free" in txt)
Output:

True

Slicing: You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.

b = "Hello, World!"
print(b[2:5])
Output:
llo

Searching Elements:
The fastest method to search through string elements is to check if the string starts or ends with the selected characters. For this, Python provides find() function for this purpose. The find function returns just instances of the specified word/characters, the rfind returns the last search results and startswith() or endswith() functions return the starting or ending portion of the string with matches with the specified keywords and returns a boolean value of true or false.

SearchMe = "The sky is blue and the grass is green!"
print(SearchMe.find("blue"))
print(SearchMe.rfind("is"))
print(SearchMe.startswith("The"))
print(SearchMe.endswith("the"))
Output:
11
30
True
False


Leave a Reply

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