You've completed expressions, conditionals, and loops. Now you'll learn STRINGS in detail.

This guide gives you:

  1. What a string is (sequence of characters)
  2. Why strings matter (words, sentences, text – used everywhere)
  3. What you'll learn next: operators on strings, and string methods (built-in functions)
  4. A few simple examples to get you ready

Let’s go! πŸš€


1. Short Summary

  • A string is text in quotes; can contain letters, digits, special characters, other languages.
  • A string is a sequence of characters; each character has an index (0, 1, 2... or -1, -2 from the end).
  • You can get one character with s[index] (square brackets).
  • Every string is of type str; type(s) gives <class 'str'>.
  • Two ways to get a string: assign in code (s = "Hi") or use input() (which always returns a string).
  • len(s) gives the number of characters.
  • for x in s: lets you loop over each character (traverse the string).
  • Use single '...' or double "..." quotes; if the string contains a quote, use the other. Multi-line β†’ triple quotes '''...''' or """...""".

2. Concepts in Simple Language + One Analogy Each

Concept A: What is a string? Simple: A string is a piece of text Python treats as one value. Whatever you put in quotes is a string. Analogy: Like a necklace of beads – each bead is a character, the whole necklace is the string.

Concept B: String as sequence and indexing Simple: Python stores a string as an ordered sequence. Each character has a position (index). First = 0, then 1, 2... From the end: last = -1, second-last = -2. Analogy: Like house numbers on a street: 0, 1, 2... or "first from the end" = -1.

Concept C: Type (str) and how we get strings Simple: All quoted text has type str. You can assign s = "Hello" or get text with input() (which returns a string). Analogy: str is the "species" of the value. You get the sentence by writing it or by asking someone (input).

Concept D: len() and for loop over a string Simple: len(s) = number of characters. for x in s: visits each character one by one. Analogy: len() = counting the beads. The for loop = walking along the necklace, looking at each bead.

Concept E: Quotes and string literals Simple: Use '...' or "...". If the text contains a quote (e.g. John's), use the other quote outside. Multi-line β†’ triple quotes. Analogy: Quotes are the "brackets" that mark where the text starts and ends; use different ones if the text has one inside.


3. Example Code Snippets

Concept A: What is a string

s = "Hello"
print("s =", s)

s = "Hi! How are you?"
print("s =", s)

s = "123"
print("type(s) =", type(s))

s = ""
print("len(s) =", len(s))

s = "Python3"
print("s[0] =", s[0], "   s[-1] =", s[-1])

Concept B: Indexing

s = "Hello"
print("s[0] =", s[0])
print("s[2] =", s[2])
print("s[-1] =", s[-1])
print("s[-2] =", s[-2])

Concept C: Type and getting strings

s = "Hi"
print("type(s) =", type(s))

name = input("Enter your name: ")  # (simulated)
print("type(name) =", type(name))

Concept D: len and for loop

s = "Hello"
print("len(s) =", len(s))

print("Characters:")
for x in s:
    print("  ", x)

Concept E: Quotes

s = "John's book"
print(s)

s = """Line one
Line two"""
print(s)

4. Common Beginner Mistakes

  • Using s(0) instead of s[0] β†’ Indexing uses square brackets [], not ().
  • Treating "123" as a number β†’ "123" is text. Use int("123") for the number.
  • Forgetting input() returns a string β†’ Use int(input(...)) when you need a number.
  • Index out of range β†’ "Hi" has indices 0 and 1 only. Check 0 to len(s)-1.
  • Wrong quotes when string has apostrophe β†’ Use "John's" not 'John's'.
  • Using 'len' as variable name β†’ Don't: len = 5 overwrites the built-in len().

5. Practice Problems (with hints)

Concept A Easy Assign your first name to a variable and print it. Then assign full name and print. Hint: Use = to assign. Use "First" + " " + "Last" or one string "First Last".

Concept B Medium s = "Hello". Print it backwards (one char per line) using a loop and negative indices. Hint: Loop i from -1 to -len(s) with step -1. Print s[i].

Concept D Medium Input a word. Print its length. Count how many times "a" appears (loop). Hint: word = input(...), len(word). Loop, counter, if x == "a".

Concept E Medium Assign a two-line string "First line" and "Second line". Print it. Print its length. Hint: Triple quotes; newline between lines. len() counts the newline too.


6. Mini-Quiz (10 questions)

Q1. What is the type of "42" in Python? Q2. What does "Hello"[1] return? Q3. What does "Hi"[-1] return? Q4. What does len("") return? Q5. What does input() return? Q6. Which is valid if the string contains an apostrophe? Q7. Output of: s = "ab"; print(s[0] + s[1]) Q8. How many times does this loop run? for x in "Hi": Q9. What does type("3.14") return? Q10. Write one line that prints the LAST character of variable 'word' without negative index (use len).


Congratulations!

You now understand the basics of Strings in Python! πŸŽ‰

Key Takeaways:

  • Strings are sequences of characters
  • Use quotes to create them and input() to get them
  • len(), indexing, and for loops work on strings
  • Python has powerful built-in methods waiting for you

Next Steps:

  • Learn string operators (+, *, in)
  • Practice methods (upper, strip, replace)
  • Start the upcoming string challenges