str
in the
Python
Standard Library
[
square
brackets]
)
and
slicing
in the
Python
Language Refernce
A
sequence
is a type of variable that holds a series of zero or more values.
For example, a
str
ing
of characters
is a type of variable that holds a series of zero or more characters.
An example of a type of variable that is
not
a sequence is an
int
.
An
int
holds only one value.
The indices of a
str
ing
start at 0.
If the
str
ing
has 16 characters,
the indices therefore go up only to 15.
You can also use negative numbers.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
c
o
u
n
t
e
r
c
l
o
c
k
w
i
s
e
-16
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
The value of s is counterclockwise The type of s is <class 'str'> The length of s is 16 The first character is c The second character is o The third character is u The sixteenth character is e The last character is e The next-to-last character is s The third-from-last character is i
For subscripting, see “Strings can be indexed” in Strings; Subscription; Common Sequence Operations.
print(s[0:7]) #counter print(s[7:12]) #clock print(s[12:16]) #wise
counter clock wise
print(s[:7]) #counter (the first 7 characters) print(s[7:]) #clockwise (all but the first 7 characters) print(s[:]) #counterclockwise (all the characters) print(s) #counterclockwise (all the characters)
counter clockwise counterclockwise counterclockwise
Why did they invent the last example ([:]
)?
See
“If you need to modify”
in
for
,
and the
subtlety.
See also the copying example in
Sort.
wise
.
print(s[12:16]) #wise print(s[12:]) #wise (all but the first 12 characters) print(s[-4:]) #wise (the last 4 characters) print(s[:-4]) #counterclock (all but the last 4 charcters)
wise wise wise counterclock
print(s[12:]) #wise print(s[12:16]) #wise print(s[12:16:1]) #wise (the stride is 1) print(s[15:11:-1]) #esiw (the stride is -1) print(s[-1:-5:-1]) #esiw (the stride is -1) print(s[-1::-1]) #esiwkcolcretnuoc (the stride is -1) print(s[::-1]) #esiwkcolcretnuoc (the stride is -1) print(reversed(s)) #esiwkcolcretnuoc
wise wise wise esiw esiw esiwkcolcretnuoc esiwkcolcretnuoc esiwkcolcretnuoc
letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" print(letters[::2]) print(letters[1::2])
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
This 9-character string is a 3 × 3 playing board for tic-tac-toe. Here are five ways to create it. All five statements create exactly the same string.
board = "012345678"
board = "012" + "345" + "678"
board = \ "012" \ + "345" \ + "678"
board = ("012" + "345" + "678")
board = ( "012" + "345" + "678")
Now that we have created the string, here’s how to print the eight straight lines that will win the game.
#The 3 rows. print(board[:3]) #012 print(board[3:6]) #345 print(board[6:]) #678 #The 3 columns. Count by 3's. print(board[::3]) #036 print(board[1::3]) #147 print(board[2::3]) #258 #The 2 diagonals. print(board[::4]) #048 upper left to lower right print(board[2:8:2]) #246 upper right to lower left
if board[:3] == "XXX": #Is the top row all X's?
if board[:3] == 3 * "X":
#Moving average. prices is a list. #Compute the average of 3 prices: prices[i-1], prices[i], prices[i+1] if sum(prices[i-1:i+2]) / 3 >= 100:
while True: i = input("What is the index of the character you want to see? ") i = int(i) print(s[i])
What is the index of the character you want to see? 12 w What is the index of the character you want to see? 14 s What is the index of the character you want to see? 16 Traceback (most recent call last): File "/Users/myname/python/subscript.py", line 28, in <module> print(s[i]) IndexError: string index out of range
IndexError
exception.
while True: i = input("What is the index of the character you want to see? ") i = int(i) try: print(s[i]) except IndexError: print(f"Sorry, index must be in the range {-len(s)} to {len(s) - 1} inclusive.")
What is the index of the character you want to see? 16 Sorry, index must be in the range -16 to 15 inclusive. What is the index of the character you want to see? 14 s What is the index of the character you want to see? etc.
ValueError
exception
here.
while True: i = input("What is the index of the character you want to see? ") try: i = int(i) except ValueError: print("Sorry, index must be an integer.") continue #Go back up to the keyword while. try: print(s[i]) except IndexError: print(f"Sorry, index must be in the range {-len(s)} to {len(s) - 1} inclusive.")
What is the index of the character you want to see? zero Sorry, index must be an integer. What is the index of the character you want to see? 0 c What is the index of the character you want to see? etc.
EOFError
exception
here.
while True: try: i = input("What is the index of the character you want to see? ") except EOFError: sys.exit(0) try: i = int(i) except ValueError: print("Sorry, index must be an integer.") continue try: print(s[i]) except IndexError: print(f"Sorry, index must be an integer in the range {-len(s)} to {len(s) - 1} inclusive.")
What is the index of the character you want to see? 0 c What is the index of the character you want to see? control-d on macOS, control-z enter on PC (the script exits)
while True: try: i = input("What is the index of the character you want to see? ") except EOFError: sys.exit(0) try: _ = float(i) except ValueError: print("Sorry, index must be a number.") continue try: i = int(i) except ValueError: print("Sorry, index must be an integer.") continue try: print(s[i]) except IndexError: print(f"Sorry, index must be an integer in the range {-len(s)} to {len(s) - 1} inclusive.")
What is the index of the character you want to see? three Sorry, index must be a number. What is the index of the character you want to see? 3.5 Sorry, index must be an integer. What is the index of the character you want to see? 3 n What is the index of the character you want to see? etc.
w
to uppercase.
s = "counterclockwise" s[12] = "W" print(s)
Traceback (most recent call last): File "/Users/myname/python/index.py", line 10, in <module> s[12] = "W" TypeError: 'str' object does not support item assignment
We therefore say that a string is immutable. To accomplish what we are trying to do, we have to build a new string:
s = "counterclockwise" s = s[:12] + "W" + s[13:] #Paste together a new s. #s = f"{s[:12]}W{s[13:]}" #another way to do the same thing. print(s)
counterclockWiseSee List of numbers for another way to make the w uppercase.