String and List

String

string is a data type, and string variables can hold sequences of text. A string literal is the actual string value used inside your python program. A programmer creates a string literal by surrounding text with single or double quotes. Ex: name = ‘Bob’, or  city = other_city, where other_city is a string variable.

The string data type is also called a sequence type because it holds characters in a specific order. For example, the word ‘Hello’ is a string, where the H is at index position 0, the e is at index 1, the l’s are at 2 and 3, and the o is a index 4.  You can access any of these characters by placing the index in brackets. Ex:

word = 'Hello'
print(word[0], word[4])

You can use the input() function to get strings from user input, while your program is running. Ex:

print('What is your name?')
name = input()
print('Hello', name)

The length of a string refers to the number of characters. For example, ‘Bob’ has a length of 3. To get the length of a string in your program, use the len() function.

Strings are immutable, which means that you cannot change the characters of a string’s value. Functions that appear to alter a string, actually just returns back an all new string, since they cannot change the original string.  You can assign a string variable to a new string or to a function call that returns a new string.

name = ‘James’
print(name.upper())
print(name)  # not uppercase this time
name = name.upper()
print(name)  # is uppercase this time

You can glue strings together using the + operator. This is called string concatenation. Ex: name = ‘Tony’ + ‘ Montana’. Note that the result of string concatenation is a new string, so the original string is not changed.

List

container is a group of related object, with references to each object rather than directly to the data. One of the most common types of containers is a list, which is similar to a grocery shopping list. Ex: shopping_list = [‘chicken’, ‘beer’, ‘nachos’].  Each item in the list is called an element. Each element in the list is numbered starting at 0, so chicken is the object at index 0, beer is at index 1 and nachos are at index 2.   You can access any element in the list by referencing the index number in square brackets. Ex: print(shopping_list[1]) will print out the string ‘beer’.

Lists can hold objects of any type. They are useful for reducing the number of variables need in your program. For example, if you wanted to store the temperatures for every day of the week, instead of having 7 separate variables like temp1, temp2, temp3 and so on, you can just have a list called temps, and access each day of the week by the index.  Ex:

temps = [85, 86, 90, 92, 88, 84, 79]
print('The temperature on Monday is', temps[1])  # prints out 86

Accessing list elements

Here is a list of integers.

exam_scores = [84, 95, 39].

If you re-took the exam you got a 39 on, and wanted to change it’s value, just access it by the index and reassign it. Ex:

print('You failed Exam 3, your score was', exam_scores[2])
exam_scores[2] = 100
print('You aced Exam 3, your score is now', exam_scores[2])

Updating list elements

Lists are mutable, which means that functions may alter or update elements in a list.  You would need to reassign a list element in order to update it’s value.

Adding and removing list elements

method is code that performs a specific action, and is called using dot notation. The append() method adds new elements to a list. Ex.

exam_scores.append(100)

Other useful methods for lists are pop() which removes an element at a specified index, and remove() which removes an element with the first matching value.

names = ['Bob', 'Tony', 'Mary', Joseph, 'Mary']
names.pop(0)  #removes Bob
names.remove('Mary')  #removes the first Mary from the list
Search
Categories
Read More
Technology
SUMIFS Function
The SUMIFS function in Excel is used to sum a range of values based on multiple criteria. It's...
By Microsoft Excel Tips 2024-10-16 01:05:32 0 2K
Education
VLOOKUP FUNCTIONS
VLOOKUP is a powerful function in Excel that allows you to search for a value in a table and...
By Microsoft Excel 2024-09-23 20:04:52 0 1K
Education
MODERN AFRICAN NATIONAL HISTORY MADE EASY
https://acrobat.adobe.com/id/urn:aaid:sc:EU:4838a935-5393-417a-a9b9-a4c21d6109cb
By Landus Mumbere Expedito 2024-07-18 10:47:57 0 2K
Computer Programming
HTML Paragraphs (<p>)
HTML paragraphs are defined by the <p> tag. They are used to group related sentences or...
By HTML PROGRAMMING LANGUAGE 2024-08-15 01:19:42 0 2K