Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON TEXT / CHAPTER 03 / Learn / LESSON 01
Chapter 3 — Text. So far we worked with numbers and structures. Now the string is the main character: a sequence of characters inside quotes.
Every character has an index. Counting starts at 0: the first character is text[0], the second is text[1]. A negative index counts from the end: text[-1] is the last character.
A slice takes part of the string: text[start:stop]. start is included, stop is not. For example "python"[0:3] gives "pyt". If you omit the end, the slice runs to the end of the string.
Mini example:
word = "code"
print(word[0])
print(word[-1])
print(word[1:3])Common mistake: expecting text[1] to be the first character; or including stop in the result ([0:3] does not include index 3). Another mistake: text[100] on a short string — IndexError.
Today we only read by index and slice. Methods like upper/split come in later lessons.
Learn: an index picks a character; a slice picks a segment.
STREAM IDLE
›