Python Strings
Python Strings
1 Python Strings
Concept: Python provides several ways to access the individual characters in a string. Strings
also have methods that allow you to perform operations on them.
statement
etc.
G I F T U n i v e r s i t y
2.2 Indexing
Another way that you can access the individual characters in a string is with an index. Each
character in a string has an index that specifies its position in the string. Indexing starts at 0, so
the index of the first character is 0, the index of the second character is 1, and so forth.
Fig:1 String Indexing
1
You can use an index to retrieve a copy of an individual character in a string.
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-03bedbfbecf9> in <module>
1 city = 'Boston'
----> 2 print(city[6])
3 String Concatenation
A common operation that performed on strings is concatenation, or appending one string to the
end of another string.The + operator produces a string that is the combination of the two strings
used as its operands.
Ali Babar
2
4 Strings Are Immutable
In Python, strings are immutable, which means that once they are created, they cannot be changed.
Some operations, such as concatenation, give the impression that they modify strings, but in reality
they do not.
Because strings are immutable, you cannot use an expression in the form string[index] on the left
side of an assignment operator. For example, the following code will cause an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-463a1d4baec1> in <module>
2 friend = 'Bill'
3 # Can we change the first character to 'J'?
----> 4 friend[0] = 'J' # No, this will cause an error!
The last statement in this code will raise an exception because it attempts to change the value of
the first character in the string ‘Bill’.
5 String Slicing
CONCEPT: You can use slicing expressions to select a range of characters from a string
A slice is a span of items that are taken from a sequence. When you take a slice from a string, you
get a span of characters from within the string. String slices are also called substrings.
To get a slice of a string, you write an expression in the following general format:
string[start : end]
Lynn
If you leave out the start index in a slicing expression, Python uses 0 as the starting index. Here
is an example:
Patty
3
If you leave out the end index in a slicing expression, Python uses the length of the string as the
end index. Here is an example:
Smith
What do you think the following code will assign to the my_string variable?
ACEGIKMOQSUWY
4
You can use the not in operator to determine whether one string is not contained in another string.
Here is an example:
5
wwwww
8 Splitting a String
Strings in Python have a method named split that returns a list containing the words in the string.
If you want to break out the month, day, and year as items in a list, you can call the split method
using the ‘/’ character as a separator, as shown here:
9 Exercise
9.1 Sum of Digits in a String
Write a program that asks the user to enter a series of single-digit numbers with nothing separating
them. The program should display the sum of all the single digit numbers in the string. For
example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4.
6
9.3 Alphabetic Telephone Number Translator
Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their
customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers
in the following fashion:
Letters Number
A, B, and C 2
D, E, and F 3
G, H, and I 4
J, K, and L 5
M, N, and O 6
P, Q, R, and S 7
T, U, and V 8
W, X, Y, and Z 9
Write a program that asks the user to enter a 10-character telephone number in the format XXX-
XXX-XXXX. The application should display the telephone number with any alphabetic characters
that appeared in the original translated to their numeric equivalent. For example, if the user enters
555-GET-FOOD the application should display 555-438-3663.
[ ]: