← Back to Hub
Module 3 Week 8 • Strings properties, indexing, and length checking

Python Strings

Master Python strings, access individual characters by index, check length with len(), and search for substrings using 'in'.

1. Creating Strings

Strings are surrounded by single quotes ' ' or double quotes " ". For multiline text blocks, use triple quotes """ """.

2. String Indexing

Strings are lists of characters. Every character has a numbered position, called an **index**, starting at **0** for the first letter:

word = "Hello"
print(word[0]) # Output: 'H'
print(word[1]) # Output: 'e'

3. String Length via len()

You can find the total count of characters in any string (including spaces and punctuation) using the built-in len() function.

4. Substring Search ('in' / 'not in')

To check if a specific word or letter exists inside a string, use the in keyword (returns True or False). Use not in to check if a word is missing.

💻 Python Code Examples

Example 1: Multiline Strings

msg = """Line 1
Line 2
Line 3"""
print(msg)

Description: Uses triple quotes to declare a string spanning multiple lines.

Example 2: Accessing characters by Index

word = "Python"
print(word[0]) # First letter
print(word[5]) # Sixth letter

Description: Queries characters at specific indices (remember indexing starts at 0).

Example 3: Negative Indexing

word = "Python"
print(word[-1]) # Output: 'n' (last letter)
print(word[-2]) # Output: 'o' (second to last)

Description: Uses negative numbers to reference characters starting from the end of the string.

Example 4: String Length Check

text = "Hello World"
print(len(text)) # Output: 11

Description: Checks length. Spaces are counted as valid characters.

Example 5: Checking Substring Existence

sentence = "The quick brown fox"
print("fox" in sentence)
print("cat" in sentence)

Description: Uses the 'in' operator to perform text searches, returning booleans.

Example 6: Checking Missing Substrings

sentence = "The quick brown fox"
print("cat" not in sentence) # True

Description: Uses 'not in' to verify that a substring is absent from the target text.

🧠 Active Retrieval Quizzes

Answer the following 10 multiple-choice questions to test your storage strength.

Question 1: What is the starting index number of the first character in a Python string?

Explanation: Like list structures, Python string indexes start at 0.

Question 2: Which function returns the total number of characters in a string?

Explanation: The len() function returns the length of a string or collection.

Question 3: What is the output of: word = 'Code'; print(word[3])?

Explanation: Indexes are C=0, o=1, d=2, e=3. Index 3 is 'e'.

Question 4: How can you check if the word 'free' is present in a variable text?

Explanation: The 'in' keyword checks for substring existence in Python.

Question 5: What index represents the very last character of a non-empty string?

Explanation: Negative index -1 always references the final character of a string in Python.

Question 6: Does len('A B') count spaces?

Explanation: Spaces are valid string characters and are included in len() counts.

Question 7: What is the result of: 'cat' not in 'dog and mouse'?

Explanation: Since 'cat' is not present in 'dog and mouse', the 'not in' check is True.

Question 8: Which quotes can be used to write strings spanning multiple lines?

Explanation: Triple quotes (single or double) let you write multiline strings in Python.

Question 9: What is the output of: word = 'Hi'; print(word[-2])?

Explanation: Negative index -1 is 'i', and -2 is 'H'.

Question 10: What happens if you try to access an index that is too large (e.g. 'Hi'[5])?

Explanation: Accessing an out-of-range index raises an IndexError.

🖥️ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

word = "Python"
print(word[1])
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print(len("AB C"))
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("x" in "xyz")
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

word = "Hello"
print(word[-1])
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("cat" not in "dog")
Console ready. Type your prediction below...

🎮 Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

📝 Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
🖥️ Output Console
Console ready.
💬 Teacher Consultation

Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.

📋 Progress Status

Lesson Complete?

📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.