3. Strings and Lists¶
3.1. Strings¶
A string is a sequence of characters, e.g.,
>>> "Hello. My name is Tom Jones"
Strings in Python are surrounded by either single quotation marks, or double quotation marks. However, you cannot combine the two quotation styles.
‘Hello’ is the same as “Hello”.
Strings can be stored as variables:
>>> y = 'Hello' is the same as "Hello".
3.2. String Operations¶
Various operations can be performed on string variables. Some of these are presented below:
a. Concatenation
Concatenating strings means joining strings by linking them end-to-end.
1print ('James' + 'Brown')
2#The output of this statement is JamesBrown.
3
4
5#Put a space after James or Brown to create the space.
6print ('James ' + 'Brown')
7
8#or
9print ('James' + ' ' + 'Brown')
10
11#or
12print ('James', 'Brown')
b Converting Non-String Objects to String
Virtually any object in Python can be rendered as a string. The str(obj) returns the string representation of object obj:
>>> str(50.2)
'50.2'
>>> str(3+4j)
'(3+4j)'
>>> str(3 + 29)
'32'
>>> str('foo')
'foo'
c. Concatenating Strings and Integers
>>> temp = 100
>>> print ("The temperature is " + temp + " degrees")
TypeError: cannot concatenate 'str' and 'int' objects
>>> print ("The temperature is " + str(temp) + " degrees")
d. Repetition
The * operator is used to perform perform repetition
>>> 'Semple ' * 3
>>> 'Semple Semple Semple'
e. Changing Cases
string.lower()
>>> text = "I LOVE WRITING PYTHON CODE"
>>> # convert message to lowercase
>>> print(text.lower())
i love writing python code
string.upper()
text = “I love learning Python”
# convert message to lowercase print(text.upper()) I LOVE LEARNING PYTHON
f. Returning the Length of a String
With len(), you can check the length of Python strings. The len(s) method returns the number of characters in the variable:
>>> s = 'I am a string.'
>>> len(s)
14
g. Formatting String Variables Using the % Operator
The program below illustrates the use of string formatting using the % operator. With this method, the percentage sign followed by a letter and some numbers indicate how a variable should be formatted. The variable itself is stored as a tuple to the right of the string. Here are some basic argument specifiers you should know:
%s - means format the variable as a string
%d - means format the variable as an integer
%f - means format the variable as as floating point number
%0.3f means format the variable as a floating point numbers with three digits to the right of the decimal value.
In the code below, %0.2f is a placeholder for a variable that will be formatted with two digits to the right of the decimal value.. The name of the variable appears at the end of the string preceded by a % sign.
1# Get inputs from the user
2base = float(input('Enter length of the base of the triangle: '))
3height = float(input('Enter the height of the triangle '))
4
5# calculate the area of the triangle
6triangle_area = (base * height) / 2
7
8# Display the results
9print ('The area of the triangle is %0.2f' % triangle_area)
When more than one variables is being formatted, a tuple is used to store the variable names at the end of the string. This is demonstrated in the code below.
1num = float(input('Enter a number: '))
2num_sqrt = num ** 0.5
3print ('The square root of %0.2f is %0.4f' %(num, num_sqrt))
h. String formatting with the format method
The .format() method is another way to format strings. With this method, we use empty curly braces {} as placeholders to store variables. In order to pass the variables the curly brackets, we use the .format() method. Within the format method, the variables are placed in the order in which they should appear in the program statement.
1fname = "John"
2lname = "Doe"'
3age = "24"
4
5print ("{} {} is {} years old." .format(fname, lname, age))
John Doe is 24 years old.
Note: You can also use:
i. String Indexing
In programming, individual items in an ordered set of data can be accessed directly using a numeric index or key value. This process is referred to as indexing.
In Python, strings are ordered sequences of character data, and thus can be indexed in this way. Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets [ ].
String indexing in Python is zero-based: the first character in the string has index 0, the next has index 1, and so on. The index of the last character will be the length of the string minus one.
A schematic diagram of the indices of the string ‘foobar’ would look like this: String index 1
The individual characters can be accessed by index as follows:
>>> s = 'foobar'
>>> s[0]
'f'
>>> s[1]
'o'
>>> s[3]
'b'
>>> len(s)
6
>>> s[len(s)-1]
'r'
Attempting to index beyond the end of the string results in an error:
>>> s[6]
- Traceback (most recent call last):
File “<pyshell#17>”, line 1, in <module> s[6] IndexError: string index out of range
String indices can also be specified with negative numbers, in which case indexing occurs from the end of the string backward: -1 refers to the last character, -2 the second-to-last character, and so on. Here are some examples of negative indexing:
>>> s = 'foobar'
>>> s[-1]
'r'
>>> s[-2]
'a'
>>> len(s)
6
>>> s[-len(s)]
'f'
j. String Slicing
Python also allows a form of indexing syntax that extracts substrings from a string, known as string slicing. If s is a string, an expression of the form s[m:n] returns the portion of s starting with position m, and up to but not including position n:
>>> s = 'foobar'
>>> s[2:5]
'oba'
Again, the second index specifies the first character that is not included in the result—the character ‘r’ (s[5]) in the example above. That may seem slightly unintuitive, but it produces this result which makes sense: the expression s[m:n] will return a substring that is n-m characters in length, in this case, 5 -2 =3.
If you omit the first index, the slice starts at the beginning of the string. Thus, s[:m] and s[0:m] are equivalent:
>>> s = 'foobar'
>>> s[:4]
'foob'
>>> s[0:4]
'foob'
Similarly, if you omit the second index as in s[n:], the slice extends from the first index through the end of the string.
>>> s = 'foobar'
>>> s[2:]
'obar'
Omitting both indices returns the original string, in its entirety.
>>> s = 'foobar'
>>> t = s[:]
k. Slicing with Negative indices
Negative indices can be used with slicing as well. -1 refers to the last character, -2, the second-to-last, and so on, just as with simple indexing. The diagram below shows how to slice the substring ‘oob’ from the string ‘foobar’ using both positive and negative indices: String index 3
Here is the corresponding Python code:
>>> s = 'foobar'
>>> s[-5:-2]
'oob'
>>> s[1:4]
'oob'
>>> s[-5:-2] == s[1:4]
True
l. Specifying a Stride in a String Slice
Adding an additional colon (:) and a third index designates a stride (also called a step), which indicates how many characters to jump after retrieving each character in the slice.
For example, for the string ‘foobar’, the slice 0:6:2 starts with the first character and ends with the last character (the whole string), and every second character is skipped. This is shown in the following diagram: String stride 1
Similarly, 1:6:2 specifies a slice starting with the second character (index 1) and ending with the last character, and again the stride value 2 causes every other character to be skipped: String stride 2
>>> s = 'foobar'
>>> s[0:6:2]
'foa'
>>> s[1:6:2]
'obr'
As with any slicing, the first and second indices can be omitted, and default to the first and last characters respectively:
>>> s = '12345' * 5
>>> s
'1234512345123451234512345'
>>> s[::5]
'11111'
>>> s[4::5]
'55555'
You can specify a negative stride value as well, in which case Python steps backward through the string. In that case, the starting/first index should be greater than the ending/second index:
>>> s = 'foobar'
>>> s[5:0:-2]
'rbo'
m. Mathematical Opeerations and Strings
In general, mathematical operations on strings are illegal:
‘2’-‘1’ ‘eggs’/’easy’ ‘third’ * ‘a charm’
3.3. Lists¶
Lists are collections of items (strings, integers, or even other lists), e.g.,
>>> y = [1,2,3, “Jim”]
Lists are widely used in GIS, e.g., list of feature classes, list of records, list of fields, list of numbers, etc.
List Creation
>>> list1 = ['one', 'two', 'three', 'four', 'five']
>>> numlist = [1, 3, 5, 7, 9]
>>> list1= [ ] # creates an empty list
3.4. List Operations¶
a. Get the length of a List
list1 = ["1", "hello", 2, "world"]
len(list1) # Use the len function
4
b. Accessing members of a list
Use the index of the value in the list surrounded by square brackets to get to the members of the list.
>>> print (list1[0], list1[1])
c. Deleting List Elements
>>> del list1[2]
d. Append values to a List
List append will add the item at the end. If you want to add at the beginning, you can use the insert function.
list = ["Movies", "Music", "Pictures"]
list.append("Documents" ) # will add "Document" to the list
list
[“Movies”, “Music”, “Pictures”, “Documents”]
e. Sorting a List
You can sort a list with the following code:
list = ["Movies", "Pictures", "Actors", "Cinemas"]
list.sort()
list
[‘Actors’, ‘Cinemas’, ‘Movies’, ‘Pictures’]
f. Summing a List
There are many ways to sum a list. The code below uses Python’s built in sum function to sum the list.
1list = [890, 786, 1234, 65, 345, 500]
2Sumlist = sum(list)
3print (Sumlist)
3820
g. Adding Two List Elements
Lists cannot be added with the simple use of an addition sign, i.e., list1 + list2. We have to iterate or loop over each element in the list, grab the corresponding values and do the addition.
1# Set up the lists
2list1 = [11, 21, 34, 12, 31, 26]
3list2 = [23, 25, 54, 24, 20, 35]
4
5
6#Create an empty list to store the sum of values at the same index position
7result_list = []
8
9#Get the length of one of the lists. If the list lengths are unequal, use the shorter list.
10list_to_iterate = len(list1)
11
12#Iterate the list adding the corresponding values at the running index from the two lists,
13# and insert the sum in a new list.
14
15
16for i in range(0, list_to_iterate):
17 result_list.append(list1[i] + list2[i])
18
19# Print resultant list
20print ("Test Result: ********** is: " + str(result_list))
21
22#We can use the same logic for subtracting, multiplying and dividing two lists.
h. Graphing a List
You can graph a list with the following code:
1import matplotlib.pyplot as plt
2
3cases = [890, 786, 1234, 65, 345, 500]
4year = (1950, 1960,1970,1980,1990,2000)
5
6plt.plot (year, cases)
7
8plt.xlabel ("Year")
9plt.ylabel ("Cases")
10plt.show()
i. List Slicing
>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
To retrieve a number from the list, just type the list name with the index number in square brackets,
# The code below prints out 5, which is the 4th number in the list counting from zero. >>> print my_list[4]
The syntax for slicing is:
listname (start, stop, step)
Examples
# Print out the numbers between 1 and 4, but not including 4.
>>> my_list[1:4]
[1,2,3]
# Print out the numbers between 1 and 8, skipping every other number.
- my_list[1:8:2]
[1,3,5,7]
i. Negative Slicing
>>> my_list[-1] # Prints out the last number in the list.
9
>>> my_list[::-1] #
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
If there is no value before the first colon, it means to start at the beginning index of the list. If there isn’t a value after the first colon, it means to go all the way to the end of the list.
3.5. Tuples¶
A List is a collection which is ordered and changeable. Allows duplicate members. On the other hand, a tuple is a collection which is ordered and unchangeable. Allows duplicate members.
A tuple is similar to a list, but the ordering of the values do not change once created. Coordinate values whose sequence must be maintained to draw a polygon can be stored as tuples.
>>> tup1 = ('physics', 'chemistry', 1997, 2000)
>>> tup2 = (1, 2, 3, 4, 5, 6, 7 )
3.6. Basic Tuples Operations¶
Tuple operations are similar to list operations
>>> len((1, 2, 3))
3
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6) - Concatenation
>>> ('Hi!',) * 4
(‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’) - Repetition
for x in (1, 2, 3):
print x,
1 2 3
Exercises
Write a program to reverse the content of the list shown below.
list1 = [100, 200, 300, 400, 500]
References