🕯️ Magic Note
The name “string” comes from a metaphor: think of characters as beads on a string. Each bead (character) has a position. You can move along the string, pick a specific bead, count the beads, or tie strings together. This is why we call text manipulation “string operations.”
| Quote Type | Example | Best For |
|---|---|---|
| Single quotes (‘) | ‘Hello’ | Most common, simple strings |
| Double quotes (“) | “Hello” | Strings that contain single quotes inside |
| Triple quotes (”’ or “””) | ”’Hello”’ | Multi-line strings |
Python
# Different ways to create strings
single = ‘Hello, Python!’
double = “Hello, Python!”
triple_single = ”’This string
can span multiple
lines easily.”’
triple_double = “””So can this one.”””
# Double quotes are useful when you need apostrophes
message = “It’s a beautiful day” # No need to escape the apostrophe
# Same string with single quotes would need an escape: ‘It\’s a beautiful day’
Python
empty1 = ”
empty2 = “”
empty3 = “””””” # Triple quotes can also be empty
print(len(empty1)) # 0 (length is zero)
print(empty1 == “”) # True
| Escape Sequence | What It Does | Example Output |
|---|---|---|
| \‘ | Single quote | ‘It\’s magic’ → It’s magic |
| \” | Double quote | “She said \”Hello\”” → She said “Hello” |
| \n | New line | “Line1\nLine2” → Line1 (new line) Line2 |
| \t | Tab (indent) | “Name:\tAli” → Name: Ali |
| \\ | Backslash itself | “Path: C:\\Users” → Path: C:\Users |
| \r | Carriage return | “Hello\rWorld” → World (overwrites Hello) |
| \b | Backspace | “Hello\bWorld” → HellWorld |
Python
# Escape sequences in action
print(‘It\’s a secret spell’) # It’s a secret spell
print(“She whispered \”Conjure\””) # She whispered “Conjure”
print(“First line\nSecond line”) # First line
# Second line
print(“Column1\tColumn2”) # Column1 Column2
print(“C:\\Users\\Feloriya”) # C:\Users\Feloriya
🕯️ Magic Note
If you have many backslashes and do not want to escape them all (like in file paths or regular expressions), you can use a raw string. Put an r before the opening quote: r”C:\Users\Feloriya”. In raw strings, backslashes are treated as normal characters.
Python
# Raw strings ignore escape sequences
normal = “C:\\Users\\Feloriya\\Desktop”
raw = r”C:\Users\Feloriya\Desktop”
print(normal == raw) # True (they contain the same characters)
print(raw) # C:\Users\Feloriya\Desktop
Python
name = “Feloriya”
print(len(name)) # 8 (F e l o r i y a)
sentence = “Hello, World!”
print(len(sentence)) # 13 (including comma, space, exclamation)
empty = “”
print(len(empty)) # 0
# Escape sequences count as ONE character
newline_str = “Hello\nWorld”
print(len(newline_str)) # 11 (H e l l o \n W o r l d → \n is one character)
Python
first = “Hello”
second = “World”
message = first + ” ” + second
print(message) # Hello World
# You can concatenate directly without variables
full = “Python ” + “is ” + “magical”
print(full) # Python is magical
🕯️ Magic Note
You cannot concatenate a string with a non-string directly. “Score: ” + 100 will raise a TypeError. You must convert the number to a string first using str(): “Score: ” + str(100).
Python
# Converting numbers to strings for concatenation
age = 25
message = “I am ” + str(age) + ” years old”
print(message) # I am 25 years old
# This would cause an error:
# message = “I am ” + age + ” years old” # TypeError
Python
echo = “ha” * 3
print(echo) # hahaha
separator = “-” * 20
print(separator) # ——————–
spaces = ” ” * 10
print(spaces + “Indented”) # Indented
# Zero or negative numbers produce an empty string
nothing = “abc” * 0
print(nothing) # (empty string)
Python
word = “Python”
# Index: 0 1 2 3 4 5
# Character: P y t h o n
# Negative: -6 -5 -4 -3 -2 -1
print(word[0]) # P
print(word[1]) # y
print(word[5]) # n
print(word[-1]) # n (last character)
print(word[-2]) # o (second last)
print(word[-6]) # P (first character)
Python
text = “Programming”
# Indices: 0 1 2 3 4 5 6 7 8 9 10
# Char: P r o g r a m m i n g
print(text[0:4]) # Prog (indices 0,1,2,3)
print(text[4:8]) # ramm (indices 4,5,6,7)
print(text[:4]) # Prog (start omitted → beginning)
print(text[8:]) # ing (end omitted → end)
print(text[:]) # Programming (entire string)
print(text[::2]) # Pormn (every second character)
print(text[::-1]) # gnimmargorP (reverse string)
🕯️ Magic Note
The slice [::-1] is a classic trick to reverse a string. It starts at the end (because step is -1) and moves backward to the beginning. This works for any sequence in Python.
Python
name = “Feloriya”
# This does NOT work (strings are immutable)
# name[0] = “M” # TypeError: ‘str’ object does not support item assignment
# This creates a NEW string and reassigns it
name = “M” + name[1:] # Creates “Meloriya”
print(name) # Meloriya
# The original “Feloriya” is now gone (or ready for garbage collection)
Python
print(“apple” == “apple”) # True
print(“apple” == “Apple”) # False (case-sensitive)
print(“apple” != “orange”) # True
print(“apple” < “banana”) # True (‘a’ comes before ‘b’)
print(“Apple” < “apple”) # True (uppercase letters have lower Unicode values)
print(“10” < “2”) # True (string comparison: ‘1’ vs ‘2’)
Python
sentence = “The magic of Python”
print(“magic” in sentence) # True
print(“Java” in sentence) # False
print(“python” in sentence) # False (case-sensitive)
# Using not in
print(“Java” not in sentence) # True
# Useful in conditionals
if “Python” in sentence:
print(“Python is mentioned!”)
Python
# input() always returns a string
user_input = input(“Enter something: “)
print(type(user_input)) # <class ‘str’>
# Convert to integer if needed
age_str = input(“Enter your age: “)
age_int = int(age_str)
# Or combine in one line (but be careful with invalid input)
height = float(input(“Enter your height in meters: “))
- Mixing quotes without escaping: ‘It’s magic’ causes SyntaxError. Use double quotes or escape: “It’s magic” or ‘It\’s magic’
- Forgetting that strings are immutable: trying to change a character directly with s[0] = ‘a’ causes TypeError
- Using + with non-strings: “Score: ” + 100 causes TypeError. Convert first: “Score: ” + str(100)
- Off-by-one errors in slicing: s[:5] gives indices 0 through 4 (not 5)
- Comparing strings with == when case differs: “hello” == “Hello” is False
- Accessing index out of range: s[10] when len(s) is 5 causes IndexError
- Forgetting that input() always returns a string, even for numbers
- How do you create a string that contains a single quote? Give two ways.
- What is the output of len(“Hello\nWorld”)?
- How do you get the last character of a string without knowing its length?
- Write a slice that extracts characters from index 2 to 5 of a string.
- What happens if you try to change a character in a string with s[0] = ‘x’?
- What does the expression “10” < “2” evaluate to? Why?
⚡ Whisper
A string is a thread of whispers sewn together. Each character holds a small piece of meaning. You can count them, slice them, join them with other threads, or ask if a certain word hides within. But you cannot unweave a single thread without cutting the whole strand. Strings are fragile in their immutability, yet strong in their persistence. Treat them with care. What you write in quotes becomes a message that Python will carry faithfully to the world. Choose your words well. They matter more than you think.