Python Programming Practice-Strings
Strings are a data type in Python for dealing with text. Python has a number of powerful features for manipulating strings.
Creating a string A string is created by enclosing text in quotes. You can use either single quotes, ‘ , or double quotes, ” . A triple-quote can be used for multi-line strings. Here are some examples:
s = ' Hello ' t = "Hello" m = """This is a long string that is spread across two lines."""
Input Recall from Chapter 1 that when getting numerical input we use an eval statement with the input statement, but when getting text, we do not use eval . The difference is illustrated below:
num = eval ( input ( ‘ Enter a number: ‘ )) string = input ( ‘ Enter a string: ‘ )
Empty string The empty string ” is the string equivalent of the number 0. It is a string with nothing in it. We have seen it before, in the print statement’s optional argument, sep= ” .
Length To get the length of a string (how many characters it has), use the built-in function len . For example,
len ( ' Hello ' )
Concatenation and repetition
The operators + and * can be used on strings. The + operator combines two strings. This operation is called concatenation. The * repeats a string a certain number of times. Here are some examples.
‘ AB ‘ + ‘ cd ‘ ‘ ABcd ‘
‘ A ‘ + ‘ 7 ‘ + ‘ B ‘ ‘ A7B ‘
‘ Hi ‘ *4 ‘ HiHiHiHi ‘
Example 1 If we want to print a long row of dashes, we can do the following
print ( ' - ' *75)
Example 2 The + operator can be used to build up a string, piece by piece, analogously to the way we built up counts and sums in Sections 5.1 and 5.2 . Here is an example that repeatedly asks the user to enter a letter and builds up a string consisting of only the vowels that the user entered.
s = '' for i in range (10): t = input ( ' Enter a letter: ' ) if t== ' a ' or t== ' e ' or t== ' i ' or t== ' o ' or t== ' u ' : s = s + t print (s)
This technique is very useful.
6.3 The in operator
The in operator is used to tell if a string contains something. For example:
if ' a ' in string: print ( ' Your string contains the letter a. ' )
You can combine in with the not operator to tell if a string does not contain something:
if ' ; ' not in string: print ( ' Your string does not contain any semicolons. ' )
Example In the previous section we had the long if condition
if t== ' a ' or t== ' e ' or t== ' i ' or t== ' o ' or t== ' u ' :
Using the in operator, we can replace that statement with the following:
if t in ‘ aeiou ‘ :
We will often want to pick out individual characters from a string. Python uses square brackets to do this. The table below gives some examples of indexing the string s= ‘ Python ‘ .
Statement Result Description
s P ﬁrst character of s s y second character of s s[-1] n last character of s
s[-2] o second-to-last character of s
• The ﬁrst character of s is s , not s . Remember that in programming, counting usually
starts at 0, not 1.
• Negative indices count backwards from the end of the string.
A common error Suppose s= ‘ Python ‘ and we try to do s . There are only six characters in the string and Python will raise the following error message:
IndexError: string index out of range
You will see this message again. Remember that it happens when you try to read past the end of a string.
A slice is used to pick out part of a string. It behaves like a combination of indexing and the range function. Below we have some examples with the string s= ‘ abcdefghij ‘ .
index: 0 1 2 3 4 5 6 7 8 9 letters: a b c d e f g h i j
Code Result Description
s[2:5] cde characters at indices 2, 3, 4 s[ :5] abcde ﬁrst ﬁve characters
s[5: ] fghij characters from index 5 to the end s[-2: ] ij last two characters s[ : ] abcdefghij entire string
s[1:7:2] bdf characters from index 1 to 6, by twos s[ : :-1] jihgfedcba a negative step reverses the string