Often, data is text and programs represent text as strings. Students should think of a string as a sequence of characters. In Python, we create a string by enclosing its characters in quotes. Both single quotes and double quotes can be used, but the opening and closing quotes must match.
Examples of how strings can be created:
book_title = 'Python Programming for Beginners' author = "Bill Gates" funny_string = "1234 PLTW @%&?" exp = "5+5"
Accessing the characters of a string
The characters, also called the elements of the string, are accessed using indexing. Consider string author as initialized above. The leftmost element is author[0]= ‘B’ and the rightmost element is author[9]= ‘s’. It is helpful to know that one can also use a negative index to access elements in strings (students may do this without realizing it). The negative index of the rightmost element is -1. See table below:
string | B | i | l | l | G | a | t | e | s | |
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
negative Index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Examples of accessing characters of a string:
author = "Bill Gates" print author[0] #Print the first element B n = len(author) #len() returns the number of elements in the string print n #prints 10 print author[n-1] #returns the last element in the string print author[-1] #Use negative index to print the last element print author[1] #returns the 2nd element print author[4] #returns a space, which is the 5th element print author[n] #Using n or any indices out of range to access an element will produce an IndexError
Many applications need to access a continuous sequence of elements in a string. This is done using an operation called slicing. The examples below illustrate how to slice a string:
book_title = 'Python Programming for Beginners' ##slice [1:4] returns the string starting at index 1, and ending at index 3. ##The 4th character is not included. book_title[1:4] #returns the string yth book_title[0:6] #returns the string Python ##You can also use negative indices in slicing book_title[-9:-1] #returns the string Beginner ##If you leave out the first number but keep the colon, ##Python assumes the first number is 0 book_title[:5] #returns Pytho ##If you leave out the second number, ##it will give you a slice from the first number to the end. book_title[7:] #returns Programming for Beginners
Summary String Operations
In addition to indexing and slicing, the operations len, +, *, in, not in, and iteration through a string are commonly used. We illustrate their use below; see CSE 1.3.5 for more examples and explanation
first_name = 'Bill' last_name = 'Gates' ##Use the len() function to get the length of a string. len(first_name) #It returns 4 ##Slicing first_name[2:4] #It returns ll ##Concatenation ##Use the operator + to concatenate two strings (i.e., "gluing" two strings together) first_name + last_name #It returns BillGates ##Repetition ##Use the operator * to create a new string, ##concatenating multiple copies of the same string first_name * 3 #It returns BillBillBill ## in/ not in ##To check if a given character or string exists/ does not exist in a given string 'a' in first_name #It returns False 'a' not in first_name #It returns True 'a' in last_name #It returns True ##Iterate through a string (e.g., to print the elements of the string in a certain way) for char in first_name: print char