## Lesson 24. String of characters Pages 50, 51 SBT Informatics 10 Connecting Knowledge to Life>

Select a sentence to see the solution faster

24.1

Assuming s = “Timetable”, what is len(s)?

A. 3. B. 5.

C. 14. D. 17.

Solution method:

Use len() to check the length of the string s = “Timetable”

Detailed explanation:

Assuming s = “Timetable”, then len(s) is equal to:

C. 14.

24.2

What values ​​do the following logical expressions return?

a) “01” prints “10101” b) “10110” prints “111000101”

c) “abcab”in”bcabcabcabcb”

Solution method:

Logical operations that result in True or False

Detailed explanation:

a) True; b) False; c) True.

24.3

Are the following two expressions equivalent?

a) only print “0123456789” b) “0” <= ch <= “9”

Solution method:

Observe two expressions and apply what you have learned

Detailed explanation:

The two expressions are equivalent

24.4

Are the following two expressions equivalent?

a) “0” <= ch <= “9” b) ord(“0”) <= ord(ch) <= ord(“9”)

Solution method:

Observe two expressions and apply what you have learned

Detailed explanation:

The two expressions are equivalent

24.5

Given the character string S, is there a way to get the string consisting of the characters taken from S but in reverse order? For example, if S = “01ab” then the received string must be “ba10”.

Solution method:

Apply knowledge and self-understanding

Detailed explanation:

There are many ways. Eg.

S1 = ” “

for ch in S:

S1 = ch + S1

or maybe with just one command:

S1 = WILL[: : -1]

24.6

If S = “1234567890” then WILL[0:4] what?

A. “123” B. “0123”

C. “01234” D. “1234”

Solution method:

S[0:4] is the command starting from position 0 to stop position -1

Detailed explanation:

If S = “1234567890” then WILL[0:4] to be:

D. “1234”

24.7

What is the output of the following program segment?

s = “0123456789”

T = “”

for i in range(0, len(S),2):

T = T + WILL[i]

print(T)

A. “” B. “02468”

C. “13579” D.”0123456789″

Solution method:

The statement means to print the elements in the sequence S starting from element 0 each element is separated by 2 hops

Detailed explanation:

The output of the following program segment is: B. “02468”

24.8

Given any character string S. Write a program with the following function:

a) Count the number of alphanumeric characters in S

b) Count the number of characters that are English letters in S.

Solution method:

Use what you have learned to write a program

Detailed explanation:

a) The program can be written as follows:

count = 0

for ch in S:

if “0” <= ch <= “9”:

count = count + 1

print(count)

b) The program can be written as follows:

count = 0

for ch in S:

if “a” <= ch <= “z” or “A” <= ch <=”Z”:

count = count + 1

print(count)

24.9

Which statement below is correct?

A. A character string in Python is a string consisting only of characters in the ASCII encoding.

B. A character string in Python is a string consisting of characters in the ASCII encoding and some Vietnamese characters in the Unicode encoding.

C. A character string in Python is a string consisting of characters in the Unicode encoding.

D. A character string in Python is a string consisting of alphanumeric characters in the Unicode encoding.

Solution method:

Based on learned knowledge about character string definition

Detailed explanation:

Correct statement:

C. A character string in Python is a string consisting of characters in the Unicode encoding.

24.10

To know if the variable s is of type string or not, what command should be checked?

Solution method:

Based on your own understanding and based on the knowledge learned

Detailed explanation:

It can be checked with the following logical expression: type(s) == type(“”).

24.11

Given two character strings s1, s2. Write a program that prints out a string of characters consisting of the characters drawn from s1, s2, respectively. If one of the two strings s1, s2 runs out first, then take it from the other string. For example, if s1 = “012”, s2 = “abcde”, the resulting string will be “0a1b2cde”.

Solution method:

Based on learned knowledge

Detailed explanation:

Let’s say m1 = len(s1), m2 = len(2), m = min(m1,m2). The program will include: two parts.

– Part 1: iterate m times, each time taking a character of s1, then take 1 character of s2, put into the result string s.

– Part 2: iterate over the remaining string (which is a string of length > m) and put the result into the string s.

24.12

Given two character strings s1, s2. Write a program that prints out a character string that is the first maximum common part of two strings s1, s2. For example, if s1 = “abcdeghik”, s2 = “abcmnopq”, then the string to be calculated is “abc”.

Solution method:

Use what you have learned to write a program

Detailed explanation:

The program can be written as follows:

m = min(len(s1),len(s2))

i = 0

s = “”

while i < m and s1[i] == s2[i]:

s = s + s1[i]

i = i + 1

print(s)