1. What are Strings?
A string in Python is a sequence of characters, used to store text.
- Creation: You can create strings by enclosing text in single quotes (`'...'`), double quotes (`"..."`), or triple quotes (`'''...'''` or `"""..."""`). Triple quotes are especially useful for multi-line strings or docstrings (documentation strings).
- Immutability: This is a crucial concept. Once a string is created in Python, it **cannot be changed**. Any operation that seems to "modify" a string (like `replace()` or `upper()`) actually creates a new string with the desired changes, leaving the original string untouched. This is a significant difference from mutable data types like lists or C-style character arrays.
# Single quotes
single_quote_str = 'UdaanPath'
print(f"Single quoted: {single_quote_str}")
# Double quotes
double_quote_str = "Learning Python is fun!"
print(f"Double quoted: {double_quote_str}")
# Triple quotes for multi-line strings
long_text = """
Welcome to the world of Python programming!
UdaanPath is here to guide your journey.
"""
print(f"Multi-line string:\n{long_text}")
# Demonstrating immutability
original_str = "hello"
modified_str = original_str.upper() # This creates a NEW string
print(f"Original: {original_str}, ID: {id(original_str)}") # id() shows memory address
print(f"Modified: {modified_str}, ID: {id(modified_str)}") # Different ID!
Single quoted: UdaanPath Double quoted: Learning Python is fun! Multi-line string: Welcome to the world of Python programming! UdaanPath is here to guide your journey. Original: hello, ID: 140730626354864 (example ID) Modified: HELLO, ID: 140730626355328 (example ID)