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
Computer Programming
Functions, Parameters, and Return Values in python
Python uses functions extensively! Here's how functions, parameters, and return values work in...
Other
Transform Someone’s Day with a Good Deed
The Ripple Effect of Kindness In a world that often feels fast-paced and self-centered, the...
By ALAGAI AUGUSTEN 2024-08-10 12:58:05 0 3K
Education
Find scholarships and study in the United States
There are countless scholarships available in the United States, offered by various institutions,...
By Mpatswe Francis 2024-08-31 18:45:10 5 3K