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?
Question 2: Which function returns the total number of characters in a string?
Question 3: What is the output of: word = 'Code'; print(word[3])?
Question 4: How can you check if the word 'free' is present in a variable text?
Question 5: What index represents the very last character of a non-empty string?
Question 6: Does len('A B') count spaces?
Question 7: What is the result of: 'cat' not in 'dog and mouse'?
Question 8: Which quotes can be used to write strings spanning multiple lines?
Question 9: What is the output of: word = 'Hi'; print(word[-2])?
Question 10: What happens if you try to access an index that is too large (e.g. 'Hi'[5])?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
word = "Python"
print(word[1])
Predict the output of the code snippet below. Type the exact output in the console.
print(len("AB C"))
Predict the output of the code snippet below. Type the exact output in the console.
print("x" in "xyz")
Predict the output of the code snippet below. Type the exact output in the console.
word = "Hello"
print(word[-1])
Predict the output of the code snippet below. Type the exact output in the console.
print("cat" not in "dog")
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
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
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.