Python Academy · Lesson

String Basics and Indexing

Learn string creation, indexing, and basic operations.

Lesson 1 of 414 steps

String Basics and Indexing is a free Python Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

Introduction

A string is an ordered sequence of Unicode characters. You can access individual characters by their position (index).

String Literals

Create strings with 'single', "double", or '''triple''' quotes. Triple quotes span multiple lines.
s = 'hello'
t = "world"
print(s, t)

Positive Indexing

s[0] is the first character, s[1] the second. Python indexes from 0. s = 'Python'; s[0] is 'P'.
s = 'Python'
print(s[0], s[3])

Negative Indexing

s[-1] is the last character, s[-2] is second-to-last. Negative indices count from the end.
s = 'Python'
print(s[-1], s[-2])

String Length

len(s) returns the number of characters. len('hello') is 5. Useful for bounds checking.
s = 'hello'
print(len(s))

Iterating Over a String

for ch in s: print(ch) loops over each character. Strings are iterable just like lists.
for ch in 'abc':
    print(ch)

in Operator

'ell' in 'hello' returns True. Use in to check substrings without importing anything.
print('ell' in 'hello')
print('xyz' in 'hello')

String Immutability

You cannot do s[0] = 'H'. Strings are immutable — any modification creates a new string.
s = 'hello'
# s[0] = 'H'  # raises TypeError
s2 = 'H' + s[1:]
print(s2)

ord() and chr() on Strings

ord(s[0]) gives the Unicode code point of the first character. chr() converts back. Useful for character math.
print(ord('A'))
print(chr(66))

Escape Sequences

\n is newline, \t is tab, \\ is a literal backslash, \' is a single quote inside single-quoted strings.
print('line1\nline2')
print('tab\there')

Raw Strings

Prefix with r to disable escape processing: r'C:\Users\name' treats backslashes literally — great for file paths and regex.
path = r'C:\Users\Alice'
print(path)

Quick Check

What does s[-1] return for s = 'Python'?

Recap

Strings are immutable Unicode sequences indexed from 0 (positive) or -1 (negative). Use len(), in, ord(), chr(), and raw strings for effective text handling.

Keep Going

Great work! Move on to the next lesson to continue building your skills.
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “String Basics and Indexing” lesson free?

Yes — the full text of “String Basics and Indexing” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “String Basics and Indexing”?

Learn string creation, indexing, and basic operations. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “String Basics and Indexing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Indexing
  2. String Slicing and Concatenation
  3. Common String Methods
  4. String Formatting with f-strings
← Back to Python Academy