A string in Python is a sequence of characters enclosed in single quotes ('Hello') or double quotes ("Hello"). It can include letters, numbers, spaces, and special symbols.
Strings are one of the most commonly used data types in Python, especially for user interaction, text processing, file handling, and displaying messages on the screen.
+.A string literal can use either single or double quotes.
Strings behave like sequences and support indexing & slicing.
Strings are immutable, so operations return new strings.
name = "Alice"
message = 'Hello, World!'
text = "Hello, World!"
print(text[0]) # H
print(text[2:5]) # llo
print(text[-1]) # !
name = "Alice"
# name[0] = "M" # ❌ This will give an error
name = "Malice" # Instead, assign a new value
msg = " Hello "
print(msg.strip()) # "Hello"
print(msg.lower()) # " hello "
print(msg.upper()) # " HELLO "
first = "Python"
second = "Rocks"
result = first + " " + second
print(result)
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
print("My name is {} and I am {} years old.".format(name, age))
print("She said, \"Hello!\"")
print("Line1\nLine2")
print("Backslash: \\\\")
txt = "Python is fun"
print(len(txt)) # string length
print(txt.find("is")) # find index
print(txt.replace("fun", "awesome"))
strip() removes outer spaces.len() returns number of characters.find() returns where substring occurs.replace() swaps text.strip() for cleaning input.len() for validation.