785.80K
Category: programmingprogramming

Strings. Indexing. Slices

1.

NIS PhM Kostanay
Strings. Indexing. Slices
Unit Unit 11.2А: Data structure.
www.bzfar.org

2.

Learning objectives:
• 11.2.2.3 apply functions and string
processing methods;
• 11.2.2.1 perform access to the elements of
strings, lists, tuples;
• 11.2.2.2 use slicers to process the string;
• 11.2.3.6 determine the difference between
different data structures.
www.bzfar.org

3.

How to create string?
fixed_word = 'Programming'
print(fixed_word)
www.bzfar.org

4.

How to create string?
fixed_word = 'Programming'
print(fixed_word)
word = input()
print(word)
www.bzfar.org

5.

How to create string?
fixed_word = 'Programming'
print(fixed_word)
word = input()
print(word)
number = 25
number_string = str(number)
print(number_string)
www.bzfar.org

6.

How to create string?
fixed_word = 'Programming'
print(fixed_word)
word = input()
print(word)
number = 25
number_string = str(number)
print(number_string)
word_plus_number = fixed_word +
number_string
print(word_plus_number)
www.bzfar.org

7.

Strings
• A string is a sequence of characters.
• You can access individual characters in a string using an index in square brackets. The string
indexing starts from 0.
• You can also access a string by negative indexing. A negative string index counts from the
end of the string.
• The indices for the elements in a string are illustrated as below:
www.bzfar.org

8.

Access Characters by Index
• # Indexing
• S = 'ABCDEFGHI'
• print(S[0])
# Prints A
• print(S[4])
# Prints E
• # Negative Indexing
• S = 'ABCDEFGHI'
• print(S[-1])
# Prints I
• print(S[-6])
# Prints D
www.bzfar.org

9.

String Concatenation
# concatenation operator
S = 'Hello,' + ' World!'
print(S)
# Hello, World!
# augmented assignment operator
S = 'Hello,'
S += ' World!'
print(S)
# Hello, World!
www.bzfar.org

10.

Find String Length
S = 'Supercalifragilisticexpialidocious'
print(len(S))
# Prints 34
www.bzfar.org

11.

Iterate Through a String
To iterate over the characters of a string, use a simple
for loop.
# Print each character in a string
S = 'Hello, World!'
for letter in S:
print(letter, end=' ')
#Hello, World!
www.bzfar.org

12.

Iterate Through an Indexes
To iterate over the indexes of a string, use a simple
for loop.
# Print each character in a string
S = 'Hello, World!'
for i in range(len(S)):
print(S[i], end=' ')
#Hello, World!
www.bzfar.org

13.

Task. Count vowels in string.
Use set:
text = 'Hello, my dear friends!'
vowels = 0
for letter in text:
if letter in {'a', 'e', 'i', 'o', 'u', 'y’}:
vowels += 1
print(vowels)
www.bzfar.org

14.

Task. Count vowels in string.
Use string:
text = 'Hello, my dear friends!'
vowels = 0
for i in range(len(text)):
if text[i] in 'aeiouy’:
vowels += 1
print(vowels)
www.bzfar.org

15.

Slicing a String
A segment of a string is called a slice and you can extract one by using a slice operator. A slice of a
string is also a string.
The slice operator [n:m] returns the part of the string from the “n-th” item to the “m-th” item,
including the first but excluding the last.
S = 'ABCDEFGHI’
print(S[2:5])
print(S[5:-1])
print(S[1:6:2])
# Prints CDE
# Prints FGH
# Prints BDF
www.bzfar.org

16.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H e
l
l
o
,
6
7
8
9
10
11
12
w o
r
l
d
!
www.bzfar.org

17.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
6
7
8
9
10
11
12
w
o
r
l
d
!
print(text[0:5])
www.bzfar.org

18.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
www.bzfar.org

19.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
www.bzfar.org

20.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
www.bzfar.org

21.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
www.bzfar.org

22.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
www.bzfar.org

23.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
print(text[7:])
www.bzfar.org

24.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
print(text[7:])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
world!
www.bzfar.org

25.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
print(text[7:])
print(text[:])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
world!
www.bzfar.org

26.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
print(text[7:])
print(text[:])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
world!
Hello, world!
www.bzfar.org

27.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
print(text[7:])
print(text[:])
print(text[11:15])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
world!
Hello, world!
www.bzfar.org

28.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
print(text[0:5])
print(text[7:12])
print(text[:5])
print(text[7:])
print(text[:])
print(text[11:15])
6
7
8
9
10
11
12
w
o
r
l
d
!
Hello
world
Hello
world!
Hello, world! d!
www.bzfar.org

29.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
www.bzfar.org

30.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
print(text[:-9])
www.bzfar.org

31.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
www.bzfar.org

32.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
www.bzfar.org

33.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
www.bzfar.org

34.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
www.bzfar.org

35.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
world!
www.bzfar.org

36.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
world!
H
www.bzfar.org

37.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
print(text[:1])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
world!
H
www.bzfar.org

38.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
print(text[:1])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
world!
H
!
www.bzfar.org

39.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[:-9])
print(text[-5:-3])
print(text[-6:])
print(text[:1])
print(text[-1:])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hell
or
world!
H
!
www.bzfar.org

40.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
print(text[0:12:2])
www.bzfar.org

41.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
www.bzfar.org

42.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
www.bzfar.org

43.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
el,wrd
www.bzfar.org

44.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
print(text[::4])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
el,wrd
www.bzfar.org

45.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
print(text[::4])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
el,wrd
Hoo!
www.bzfar.org

46.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
print(text[::4])
print(text[::-1])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
el,wrd
Hoo!
www.bzfar.org

47.

String slices
text = 'Hello, world!'
0
1
2
3
4
5
H
e
l
l
o
,
-13
-12
-11
-10
-9
-8
print(text[0:12:2])
print(text[1:12:2])
print(text[::4])
print(text[::-1])
6
-7
7
8
9
10
11
12
w
o
r
l
d
!
-6
-5
-4
-3
-2
-1
Hlo ol
el,wrd
Hoo!
!dlrow ,olleH
www.bzfar.org

48.

Modify a String
S = 'Hello, World!'
S[0] = ‘J’ # Triggers TypeError: 'str' object does
not support item assignment
S = 'Hello, world!’
new_S = 'J' + S[1:]
print(new_S)# Prints Jello, world!
www.bzfar.org

49.

Modify a String
S = 'Hello, World!'
S[0] = ‘J’ # Triggers TypeError: 'str' object does
not support item assignment
Immutable
www.bzfar.org

50.

Modify a String
S = 'Hello, World!'
S[0] = ‘J’ # Triggers TypeError: 'str' object does
not support item assignment
Decision
S = 'Hello, world!’
new_S = 'J' + S[1:]
print(new_S)# Prints Jello, world!
www.bzfar.org

51.

Storing symbols in memory
print(ord('Б'))
print(ord('А'))
print(ord('Г'))
# code in 1041
www.bzfar.org

52.

Storing symbols in memory
print(ord('Б'))
print(ord('А'))
print(ord('Г'))
# code in memory 1041
# code in memory 1040
# code in memory 1043
www.bzfar.org

53.

Storing symbols in memory
print(ord('Б'))
print(ord('А'))
print(ord('Г'))
print(chr(1044))
print(chr(1046))
# code in memory 1041
# code in memory 1040
# code in memory 1043
www.bzfar.org

54.

Storing symbols in memory
print(ord('Б'))
# code in memory 1041
print(ord('А'))
# code in memory 1040
print(ord('Г'))
# code in memory 1043
print(chr(1044))

print(chr(1046))

www.bzfar.org

55.

What fragment of code output?
for i in range(26):
print(chr(ord('A') + i), end=',’)
A – English letter
www.bzfar.org

56.

What fragment of code output?
for i in range(26):
print(chr(ord('A') + i), end=',')
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,
W,X,Y,Z,
www.bzfar.org

57.

Solving tasks
• Stepik.org
• 5.2, 5.4, 5.3
www.bzfar.org
English     Русский Rules