Karinoya Learning Room

Qualifications · Cloud / AI / Python Success Lab

Python Types and Strings

Read the questions and explanations in English. The lectures (explanatory articles) are available in Japanese only.

View the Japanese version (with lectures) →

Q1 | Main textbook

Which book is the correct main textbook for the Python 3 Certified Engineer Basic Exam?

  1. 『Pythonチュートリアル』(Python Tutorial), published by O'Reilly Japan
  2. 『あたらしいデータ分析の教科書』(The New Textbook of Data Analysis), published by Shoeisha
  3. 『Pythonデータ分析 実践ハンドブック』(Python Data Analysis Practical Handbook), published by Impress
  4. 『Python実践レシピ』(Python Practical Recipes), published by Gijutsu-Hyoronsha
AnswerA. 『Pythonチュートリアル』(Python Tutorial), published by O'Reilly Japan

The main textbook for the Basic Exam is O'Reilly Japan's 『Pythonチュートリアル』(Python Tutorial), and the official page states that it has corresponded to the 4th edition since September 1, 2021. The exam scope is also published using this book's chapter numbers and chapter titles, so preparation takes the form of covering the book's content in proportion to each chapter's weight. Gijutsu-Hyoronsha's 『Python実践レシピ』(Python Practical Recipes) is the main textbook for the Practical Exam, Shoeisha's 『Pythonによるあたらしいデータ分析の教科書』(The New Textbook of Data Analysis with Python) is the main textbook for the Data Analysis Exam, and Impress's 『Pythonデータ分析 実践ハンドブック』(Python Data Analysis Practical Handbook) is the main textbook for the Data Analysis Practical Exam — all of these are for different exams run by the same association. Mixing up the textbooks means endlessly studying material outside the scope, so it is worth confirming this first.

Q2 | The chapter with the most questions

In the Basic Exam's scope, which chapter of the main textbook has the most questions drawn from it?

  1. Chapter 5, "Data Structures"
  2. Chapter 4, "More Control Flow Tools"
  3. Chapter 3, "An Informal Introduction to Python"
  4. Chapter 8, "Errors and Exceptions"
AnswerB. Chapter 4, "More Control Flow Tools"

The chapter with the most is Chapter 4, "More Control Flow Tools," with 9 of the 40 questions assigned to it. Next comes Chapter 5, "Data Structures," with 7, followed by Chapter 3, "An Informal Introduction to Python," with 6, and Chapter 8, "Errors and Exceptions," with 4. Chapters 3, 4, and 5 alone add up to 22 questions, more than half of the total. It is therefore rational to spend more study time on these three chapters, and putting off Chapter 4, which covers the for statement and function definitions, in particular can be hard to recover from.

Q3 | Distribution by chapter

In the Basic Exam's scope, what share does Chapter 4, "More Control Flow Tools," occupy?

  1. 17.5%
  2. 10.0%
  3. 22.5%
  4. 15.0%
AnswerC. 22.5%

Chapter 4, "More Control Flow Tools," is 9 of the 40 questions, which comes to 22.5 percent. It is the largest by chapter, followed by Chapter 5, "Data Structures," with 7 questions, 17.5 percent. 15.0 percent corresponds to Chapter 3, "An Informal Introduction to Python," with 6 questions, and 10.0 percent corresponds to Chapter 8, "Errors and Exceptions," and Chapter 10, "Brief Tour of the Standard Library," each with 4 questions. This breakdown is published as the exam scope itself, not as an operational statistic, so it can be used as the basis for a study plan.

Q4 | Chapter 13's allocation

In the Basic Exam's scope, how many questions are assigned to Chapter 13, "What Now?"

  1. 0 questions
  2. 1 question
  3. 4 questions
  4. 2 questions
AnswerA. 0 questions

Chapter 13, "What Now?," has 0 questions, making it the only chapter in the exam scope from which nothing at all is drawn. Since its content guides the reader's next step, it is excluded as an object for an exam that tests grammar. 1 question is assigned to Chapters 1, 2, 7, 11, 12, and 14; 2 questions to Chapters 6 and 9; and 4 questions to Chapter 8, "Errors and Exceptions," and Chapter 10, "Brief Tour of the Standard Library." Since every chapter besides 13 has at least 1 question, it is worth preparing at least by skimming the terminology even for chapters with a small allocation.

Q5 | print's sep

When the following program is run, which string is printed to the screen?

print('Python', 3, 'exam', sep='/')
  1. Python3exam
  2. Python 3 exam
  3. Python/3/exam/
  4. Python/3/exam
AnswerD. Python/3/exam

print() joins the arguments it receives using the keyword argument sep. The default for sep is a single half-width space, so without specifying it, the result is Python 3 exam, but here sep='/' is specified, so the pieces are joined with slashes, giving Python/3/exam. Python3exam is the output when sep is set to an empty string. Choosing Python/3/exam/, with a separator added at the end too, happens if you think sep is placed after every element; in reality it is inserted only between arguments. What gets placed at the end is end, instead.

Q6 | print's end

When the following three lines are run in order from the top, what appears on the screen?

print('A', end='')print('B', end='')print('C')
  1. AB and C are printed on 2 lines
  2. ABC is printed as a single line
  3. A B C is printed as a single line
  4. A, B, C are printed on 3 lines
AnswerB. ABC is printed as a single line

print() attaches the keyword argument end to the end of its output. The default is a newline character, so without specifying it, a newline is added every time print() is called. Here, end='' is used for the first two, stopping the newline, so A and B are printed back to back, and the last print() adds the default newline, so ABC is printed as a single line. A B C, with spaces inserted, happens when you pass all three as arguments to a single print() call; being split across 3 lines happens if end is never specified at all; and being split into 2 lines happens if end='' is removed from just one of them.

Q7 | Unpacked output

What is the output of the following program, which unpacks a list to display it?

nums = [10, 20, 30]print(*nums, sep=', ')
  1. 10, 20, 30 is printed
  2. 102030 is printed
  3. [10, 20, 30] is printed
  4. 10 20 30 is printed
AnswerA. 10, 20, 30 is printed

print(*nums), with an asterisk attached, passes each element of the list to print() as a separate argument. So three arguments are joined with sep, and since sep=', ', the result is 10, 20, 30. Writing print(nums) without the asterisk passes the list as a single argument, so it comes out as is with square brackets, [10, 20, 30]. Without specifying sep, the default half-width space gives 10 20 30, and with sep='' it comes out packed together as 102030.

Q8 | Comment marker

Which of the following correctly describes Python comments?

  1. The range enclosed by /* and */ is ignored as a comment
  2. Everything from # to the end of the line is ignored as a comment
  3. Everything from -- to the end of the line is ignored as a comment
  4. Everything from // to the end of the line is ignored as a comment
AnswerB. Everything from # to the end of the line is ignored as a comment

A Python comment is written with a hash sign, and everything from there to the end of the line is ignored. If placed at the start of a line, the whole line becomes a comment; if placed partway through, everything from there onward does. There is no dedicated syntax for spanning multiple lines, so a hash is placed on each line. The style of enclosing text in triple quotes is sometimes used as a substitute for a block comment, but syntactically that is a string literal, and if placed at the start of a function or class it is kept at runtime as a docstring. A double slash is Python's floor-division operator, and a slash-asterisk pair or a double hyphen is a comment syntax from other languages.

Q9 | Indentation

When the following program is saved to a file and run, what happens?

if True:    print('one')        print('two')
  1. Only two is printed, and processing ends
  2. Only one is printed, and processing ends
  3. one and two are printed in this order
  4. An IndentationError occurs and it stops
AnswerD. An IndentationError occurs and it stops

Since Python represents a block by its indentation depth, statements belonging to the same block must have matching indentation width. Only the third line is indented more deeply, and the preceding line is not a line that introduces a new block (one ending with a colon), so this becomes IndentationError: unexpected indent. This is detected during the syntax parsing that happens before execution, so even the print on the second line is not run, and not even one gets printed. Failing to indent where indentation is required gives a different message, expected an indented block, and the width no longer matching partway through gives unindent does not match any outer indentation level, but both are still an IndentationError.

Q10 | Character encoding

Which is the correct description regarding the character encoding of a Python 3 source file?

  1. Shift_JIS is the default, and it can be changed with a declaration
  2. UTF-8 is the default, and Japanese can be written as is
  3. EUC-JP is the default, and Japanese can be written as is
  4. ASCII is the default, and a declaration is needed to include Japanese
AnswerB. UTF-8 is the default, and Japanese can be written as is

A Python 3 source file is read as UTF-8 by default, so Japanese can be written as is in both comments and string literals. In Python 2, the default was ASCII, and to include Japanese you had to write a coding declaration on the first or second line of the file, but in Python 3 that declaration became unnecessary. Writing the declaration is needed only when you want it to read a file saved with a character encoding other than UTF-8. Note that Japanese can also be used in identifiers (variable and function names), though this is not recommended from a readability standpoint.

Q11 | The type of division

The following program handles division between integers, printing a value on line 1 and the name of its type on line 2. Which is the correct combination?

print(9 / 3)print(type(9 / 3).__name__)
  1. The value is 3.0, and the type is int
  2. The value is 3.0, and the type is float
  3. The value is 3, and the type is int
  4. The value is 3, and the type is float
AnswerB. The value is 3.0, and the type is float

A single slash, /, is true division, and its result is always a float. This holds even when it divides evenly, so 9 / 3 is not 3 but 3.0, and its type is float too. If you think it becomes int because it is a computation between integers, you would pick 3 and int, but when you want an int, you need to use floor division and write 9 // 3, which gives 3 and int. Combinations where the look of the value and its type disagree, such as 3 and float, or 3.0 and int, do not arise from this expression.

Q12 | Floor division of negative numbers

What is the output of the following program, which lines up floor divisions?

print(9 // 4)print(-9 // 4)
  1. Line 1 is 2.0, line 2 is -3.0
  2. Line 1 is 2, line 2 is -3
  3. Line 1 is 2.25, line 2 is -2.25
  4. Line 1 is 2, line 2 is -2
AnswerB. Line 1 is 2, line 2 is -3

A double slash, //, is floor division, and it rounds the quotient down, that is, toward the smaller side on the number line. Since 9 / 4 is 2.25, 9 // 4 is 2; and since -9 / 4 is -2.25, -9 // 4 becomes not -2 but -3. The key point is that it does not round toward 0; choosing -2 happens if you think it rounds toward 0 the way C does. 2.25 and -2.25 are the values you get using / instead of //, and 2.0 and -3.0 are the values you get if you mix in a float operand and write 9.0 // 4 — when both sides are int, the result is int too.

Q13 | divmod

What value does divmod(-11, 3) return?

  1. (-4, 1)
  2. (-3, -2)
  3. (-4, -2)
  4. (-3, 2)
AnswerA. (-4, 1)

divmod(a, b) returns the tuple (a // b, a % b). Since Python's // rounds down, -11 // 3 becomes -4, and since the sign of the remainder matches the right-hand side, -11 % 3 becomes 1. So the answer is (-4, 1). If you think in terms of rounding toward 0, you get a quotient of -3 and a remainder of -2, giving (-3, -2), but Python does not use that approach. The quotient and remainder always maintain the relationship that the divisor times the quotient plus the remainder equals the original value, so you can check with 3 times -4 plus 1 equaling -11. Checking with this relationship, neither (-3, 2) nor (-4, -2) returns to -11.

Q14 | int truncation

What is the output of the following program, which compares directions of truncation?

print(int(-2.7))print(-2.7 // 1)
  1. Line 1 is -2, line 2 is -2.0
  2. Line 1 is -3, line 2 is -3.0
  3. Line 1 is -2, line 2 is -3.0
  4. Line 1 is -3, line 2 is -2.0
AnswerC. Line 1 is -2, line 2 is -3.0

The built-in function int() discards the fractional part and rounds toward 0, so int(-2.7) becomes not -3 but -2. Meanwhile, // rounds down, so -2.7 // 1 goes toward -3, and since a float operand is involved, the result is also a float, -3.0. For positive numbers, both int(2.7) and 2.7 // 1 go toward 2, so the difference does not show, but for negative numbers the directions split. Neither of these rounds to the nearest, so if you want to turn -2.7 into -3, you should use round(-2.7).

Q15 | Decimal error

What is the output of the following program, which prints Boolean values on two lines?

print(0.1 + 0.7 == 0.8)print(round(0.1 + 0.7, 2) == 0.8)
  1. Line 1 is False, line 2 is False
  2. Line 1 is True, line 2 is True
  3. Line 1 is True, line 2 is False
  4. Line 1 is False, line 2 is True
AnswerD. Line 1 is False, line 2 is True

Because float holds decimals in binary, values that are simple in decimal, such as 0.1 or 0.7, cannot be represented exactly. In fact, printing 0.1 + 0.7 gives 0.7999999999999999, which is slightly different from 0.8, so line 1 is False. Line 2 rounds to 2 decimal places with round() before comparing, so both sides become 0.8 and it is True. This is not a defect unique to Python but a property of IEEE 754 binary floating point, the same reason 0.1 + 0.2 becomes 0.30000000000000004 and 1.1 + 2.2 becomes 3.3000000000000003. Avoid equality comparisons on floats; instead compare after rounding to the needed number of digits, compare the absolute difference against a tolerance, or, in a case such as money where exactness is required, use the decimal module.

Q16 | round's rounding

What is the output of the following program, which lines up rounded values on one line?

print(round(1.5), round(2.5), round(3.5))
  1. 2 2 4
  2. 2 3 4
  3. 1 3 3
  4. 1 2 3
AnswerA. 2 2 4

Python's round() rounds to the nearest even number, so-called banker's rounding. Only for values that are exactly halfway does it move to the nearer even number, so 1.5 becomes 2, 2.5 becomes 2, and 3.5 becomes 4, giving the output 2 2 4. The rounding taught in school would give 2 3 4, but Python does not use that method. 1 2 3 is the value if you simply truncate the fractional part, and 1 3 3 is the value if you round toward the odd side; neither matches the actual behavior. Note that round() with its second argument omitted returns an int, while specifying the number of digits, as in round(2.5, 0), returns the float 2.0.

Q17 | bool is int

What is the output of the following program, which treats Boolean values as numbers?

print(True + True)print(isinstance(True, int))
  1. Line 1 is True, line 2 is True
  2. Line 1 is 2, line 2 is False
  3. Line 1 is 2, line 2 is True
  4. Line 1 is True, line 2 is False
AnswerC. Line 1 is 2, line 2 is True

Because bool inherits from int, True can be used directly in numeric operations as the integer 1, and False as the integer 0. True + True is 1 plus 1, giving 2, and isinstance(True, int) returns True. Thinking line 1 is True happens if you mistake + for logical OR; Python's logical OR is or, and True or True is True. issubclass(bool, int) is also True, and sum([True, False, True]) becoming 2 is for the same reason. This property is often used to count how many conditions were satisfied.

Q18 | Integer precision

Which is the correct description of the Python 3 int type?

  1. It has no upper limit on the number of digits and can handle values as large as memory allows
  2. Values beyond 32 bits can overflow and become negative
  3. An OverflowError is raised once the number of digits exceeds an upper limit
  4. Beyond 64 bits it is automatically converted to float for the calculation
AnswerA. It has no upper limit on the number of digits and can handle values as large as memory allows

Python 3's int has arbitrary precision, with no upper limit on the number of digits. 3 ** 40 is computed exactly as 12157665459056928801, which is beyond the range of a signed 64-bit value, but its type remains int. It can also handle a value such as 2 ** 1000, and counting its digits with len(str(2 ** 1000)) gives 302 digits. Since it is not a fixed-length integer like C's, it never overflows into a negative value, nor does it switch to float on its own. OverflowError comes out for things like exceeding the range float can represent; it is not raised just because an int's digit count grows. However, more digits does mean more computation time and memory.

Q19 | Indexing bytes

What are the outputs on line 2 and line 3 of the following program, which handles a byte string?

b = 'abc'.encode()print(b[0])print(b[0:1])
  1. Line 1 is b'a', line 2 is b'a'
  2. Line 1 is 97, line 2 is b'a'
  3. Line 1 is 97, line 2 is 97
  4. Line 1 is a, line 2 is b'a'
AnswerB. Line 1 is 97, line 2 is b'a'

Accessing bytes with an integer index returns the byte value at that position as an int. The first byte of b'abc', which is 'abc' encoded in UTF-8, is the code point of 'a', so it is 97. Taking a slice instead returns a bytes object of length 1, so b[0:1] is b'a'. With str, s[0] and s[0:1] both give the same str, 'a', so this is a point where str and bytes contrast. The display coming out as b'a' rather than a is because print() shows bytes in repr form; to get it back as a string, use b.decode().

Q20 | Radix and characters

What is the output of the following program, which lines up base conversion and character-code conversion?

print(int('1010', 2), bin(10), hex(255), ord('A'))
  1. 10 0b1010 0xff 65
  2. 10 0b1010 0xFF 65
  3. 10 0b1010 0xff 97
  4. 1010 0b1010 0xff 65
AnswerA. 10 0b1010 0xff 65

The second argument of int() is the base, so int('1010', 2) reads the string as base-2 and returns 10. Thinking it comes out as 1010 unchanged happens if you overlook the specified base. bin() returns a string with the 0b prefix, '0b1010', and hex() returns a lowercase string with the 0x prefix, '0xff'. It never becomes uppercase 0xFF; when you want uppercase notation, use format(255, 'X'). ord() returns a character's code point, and the code point of uppercase A is 65. 97 is the code point of lowercase a; the reverse conversion is chr(65), which gives back 'A'.

Q21 | Slicing

What is the output of the following program, which handles string slicing?

s = 'Pythonic'print(s[2:5])print(s[-3:])
  1. Line 1 is tho, line 2 is nic
  2. Line 1 is tho, line 2 is ic
  3. Line 1 is thon, line 2 is nic
  4. Line 1 is yth, line 2 is nic
AnswerA. Line 1 is tho, line 2 is nic

s[2:5] takes elements starting at index 2 up to but not including index 5. Since index 2 of 'Pythonic' is t, the three characters t, h, and o give tho. Because the end position is not included, it does not become thon. If you read the index as counting from 1, as if starting "from the 2nd character," you would pick yth, which corresponds to s[1:4]. s[-3:] uses a negative index, so it counts from the end, taking the character 3 from the end, n, as the start point through to the end, giving nic. Misreading -3 as "up to the 3rd character from the end" leads you to pick ic, which corresponds to s[-2:]. Keeping in mind that indices start at 0, and that negative indices count from the end with -1 as the last one, avoids confusion.

Q22 | Out of range and in

What is the output of the following program, which displays two check results?

s = 'Python'print(s[100:200] == '')print('py' in s)
  1. Line 1 is True, line 2 is False
  2. Line 1 is True, line 2 is True
  3. Line 1 is False, line 2 is True
  4. Line 1 is False, line 2 is False
AnswerA. Line 1 is True, line 2 is False

A slice does not raise an exception even if its range exceeds the length of the string; it just returns the overlapping part. 'Python' has only 6 characters, so s[100:200] becomes an empty string, and comparing it to an empty string gives True. This contrasts with a single index, s[100], which would raise IndexError. On line 2, the in operator checks for the presence of a substring, but it is case-sensitive, so the lowercase 'py' is not contained in 'Python' and gives False. 'Py' or 'th' would give True.

Q23 | Strings are immutable

When the following program is written to a file and run, what happens?

s = 'abc's[0] = 'z'print(s)
  1. A TypeError occurs and the assignment fails
  2. It is replaced with zbc and printed
  3. An IndexError occurs and the assignment fails
  4. It is printed unchanged, as abc
AnswerA. A TypeError occurs and the assignment fails

str is immutable, meaning its contents cannot be changed after it is created, so you cannot rewrite just one character using an index. s[0] = 'z' results in TypeError: 'str' object does not support item assignment, and execution stops there, so print is not even run. IndexError is what occurs when an index is out of range, but here 0 is within the valid range, so that is a different matter. When you want to change the contents, you build and reassign a new string, such as s = 'z' + s[1:]. A list is mutable, so you can rewrite an element the same way.

Q24 | A method's return value

What is the output of the following program, which calls string methods?

s = 'exam's.upper()print(s)print(s.replace('e', 'E'))
  1. Line 1 is EXAM, line 2 is Exam
  2. Line 1 is EXAM, line 2 is EXAM
  3. Line 1 is exam, line 2 is exam
  4. Line 1 is exam, line 2 is Exam
AnswerD. Line 1 is exam, line 2 is Exam

A string method does not rewrite the original string; it creates and returns a new string. On line 2, s.upper() creates 'EXAM', but since it is not assigned anywhere, that value is discarded and s remains 'exam'. So print(s) prints exam. Line 4 directly displays the return value of s.replace('e', 'E'), so Exam comes out, but this too does not change s itself. To keep the result, you reassign, as in s = s.upper(). This contrasts with a method like a list's sort(), which reorders in place and returns None.

Q25 | f-string

What is the output of the following program, which displays with a format specified?

n = 2.71828print(f'{n:.2f}')print(f'{"ok"!r}')
  1. Line 1 is 2.71, line 2 is 'ok'
  2. Line 1 is 2.72, line 2 is ok
  3. Line 1 is 2.718, line 2 is ok
  4. Line 1 is 2.72, line 2 is 'ok'
AnswerD. Line 1 is 2.72, line 2 is 'ok'

The f-string format specifier :.2f rounds to 2 decimal places for display, so 2.71828 becomes 2.72. Since it is not truncation, it does not become 2.71, and 2.718 is the value you would get with :.3f. On line 2, !r is a conversion specifier that displays the value in repr() form. Used on a string, it adds quotes, giving 'ok'; without !r, only ok comes out. The format specifier syntax is shared with str.format(), so writing '{n:.2f}'.format(n=2.71828) gives the same 2.72.

Q26 | split

What is the output of the following program, which splits strings?

print('a b  c'.split())print('a,b,c'.split(',', 1))
  1. ['a', 'b', 'c'] and ['a', 'b', 'c']
  2. ['a', 'b', '', 'c'] and ['a', 'b,c']
  3. ['a', 'b', 'c'] and ['a', 'b,c']
  4. ['a', 'b', '', 'c'] and ['a', 'b', 'c']
AnswerC. ['a', 'b', 'c'] and ['a', 'b,c']

Calling split() with no arguments treats runs of consecutive whitespace as a single separator and does not create empty elements. So 'a b c'.split() becomes the 3-element ['a', 'b', 'c']. When you specify the separator explicitly and write 'a b c'.split(' '), it splits on each single space, so an empty string remains, giving the 4-element ['a', 'b', '', 'c']. The second argument is maxsplit, representing an upper limit on the number of splits; passing 1 splits only the first time, giving ['a', 'b,c'], with the rest left unsplit as one element. Omitting it splits everything, giving ['a', 'b', 'c'].

Q27 | strip

What is the output of the following program, which strips characters from both ends?

print('xxabxx'.strip('x'))print('xxabxx'.lstrip('x'))
  1. Line 1 is abxx, line 2 is ab
  2. Line 1 is ab, line 2 is abxx
  3. Line 1 is xabx, line 2 is abxx
  4. Line 1 is ab, line 2 is xxab
AnswerB. Line 1 is ab, line 2 is abxx

The argument to strip() is "a set of characters to remove," and it keeps stripping from both ends as long as a character in that set continues. It is not removing a single occurrence of the substring 'x', so 'xxabxx'.strip('x') drops all the x's from both ends, giving ab. If you think it only removes one character at a time, you would pick xabx. lstrip() only targets the left end, giving abxx, and rstrip() only the right end, giving xxab. Omitting the argument strips whitespace characters. Because it is treated as a set, writing 'xxabxx'.strip('xa') drops the leading xx and the a too, leaving only b.

Q28 | join and repetition

What is the output of the following program, which joins strings?

print('-'.join('abc'))print('ab' * 3)
  1. Line 1 is -a-b-c-, line 2 is ababab
  2. Line 1 is abc, line 2 is ababab
  3. Line 1 is a-b-c, line 2 is ab ab ab
  4. Line 1 is a-b-c, line 2 is ababab
AnswerD. Line 1 is a-b-c, line 2 is ababab

join() uses the calling string as the separator, joining together each element of the passed iterable. A string is also an iterable you can take one character at a time from, so '-'.join('abc') joins 'a', 'b', and 'c' with hyphens to give a-b-c. The separator is only inserted between elements, not before or after, so it does not become -a-b-c-. Getting abc, with no separator, happens if you write ''.join('abc'). The * operator repeats a string a specified number of times, so 'ab' * 3 gives ababab, with no space inserted between. If you want ab ab ab, you write ' '.join(['ab'] * 3).

Q29 | find and index

What is the output of the following program, which searches for a substring?

print('banana'.find('z'))print('banana'.index('n'))
  1. Line 1 is -1, line 2 is 2
  2. Line 1 is -1, line 2 is 3
  3. Line 1 is -1, line 2 is 4
  4. Line 1 is 0, line 2 is 2
AnswerA. Line 1 is -1, line 2 is 2

find() returns -1 when the substring is not found, and it does not raise an exception. There is no 'z' in 'banana', so it is -1. 0 is the value when it is found at the very start, so take care not to mix that up with the not-found case. index() is the same as find() in that it returns the position where it was found; the first occurrence of 'n' within 'banana' is at index 2, so it is 2. Getting 3 happens if you count the index from 1 and read it as "the 3rd character," and 4 is the position of the last occurrence, which you would get with rfind() or rindex(). The only difference between the two is what happens when it is not found: index() raises ValueError: substring not found and stops processing.

Q30 | Raw strings

What is the output of the following program, which counts characters?

print(len(r'a\nb'), len('a\nb'))
  1. 3 4
  2. 4 4
  3. 3 3
  4. 4 3
AnswerD. 4 3

In a raw string, made by putting r before the literal, a backslash is not interpreted as the start of an escape sequence. r'a\nb' is the 4 characters a, backslash, n, and b, so its len is 4. Meanwhile, in the ordinary string 'a\nb', \n is replaced with a single newline character, so it is the 3 characters a, newline, and b, giving a len of 3. Counting both as escapes gives 3 3, and counting both as raw characters gives 4 4, but the key point is that the presence of r changes the handling. Even with r, the type remains an ordinary str; it does not become a special type. Note that len('\\') is 1, since two backslashes are needed to write a single literal backslash character.

Practice: answer the questions on this page

This practice tool asks questions in random order (it works when JavaScript is enabled). You can still read all the questions and explanations above without it.

* The explanations are information for study purposes. Exam scope and systems change from year to year, so always check the official announcements of the organization that administers the exam.

This page is a translation of the Japanese original. If the translation and the original differ, the Japanese version takes precedence. View the Japanese original