Loading notes...
Loading notes...
BCA • Chapter 5
Chapter 5 covers the string data type in Python, focusing on immutability, index-based slicing, a wide array of built-in manipulation methods, and modern f-string formatting.
Strings are one of the most widely used data types in Python. A string is simply a sequence of characters. From processing user input and parsing web pages to handling file paths and displaying formatted output, strings are everywhere. This chapter explores how to create strings, slice them, manipulate them using built-in methods, and format them beautifully.
5.1 String Creation and Basics
In Python, strings can be created by enclosing characters inside single quotes `''`, double quotes `""`, or triple quotes `""""""` or `''''''`. Triple quotes are specifically used for multi-line strings or docstrings.
A crucial concept to remember: Strings in Python are IMMUTABLE. This means that once a string is created, you cannot change its individual characters in place. Any operation that modifies a string actually creates a brand new string.
s1 = 'Hello' # Single quotes
s2 = "World" # Double quotes
s3 = """This is a
multi-line string."""
# Immutability Example
text = "Cat"
# text[0] = 'B' # This will raise a TypeError!
# You must create a new string instead:
text = "B" + text[1:]
print(text) # Output: Bat5.2 String Indexing and Slicing
Because a string is a sequence, every character has a specific numerical position called an index. Python uses zero-based indexing (the first character is at index 0). Furthermore, Python supports negative indexing, where `-1` represents the very last character.
Slicing allows you to extract a substring. The syntax is `string[start:stop:step]`.
word = "Programming"
# Indexing
print(word[0]) # Output: P (First character)
print(word[-1]) # Output: g (Last character)
# Slicing: [start:stop]
# Grabs from 'start' up to, but not including, 'stop'
print(word[0:4]) # Output: Prog
print(word[3:]) # Output: gramming (From index 3 to the end)
print(word[:7]) # Output: Program (From start up to index 7)
# Slicing with Step: [start:stop:step]
print(word[::2]) # Output: Pormig (Every 2nd character)
print(word[::-1]) # Output: gnimmargorP (Reverses the string!)5.3 Built-in String Methods
Python strings come packed with highly optimized built-in methods. Since strings are immutable, these methods always return a new string, leaving the original unchanged.
s = " hello Python world "
# Cleaning and Modification
print(s.strip()) # "hello Python world" (Removes leading/trailing whitespace)
print(s.replace("hello", "Hi"))# " Hi Python world "
# Case Conversion
clean_s = s.strip()
print(clean_s.upper()) # "HELLO PYTHON WORLD"
print(clean_s.title()) # "Hello Python World"
# Splitting and Joining
words = clean_s.split(" ") # Splits string into a list: ['hello', 'Python', 'world']
joined = "-".join(words) # Joins list into a string: "hello-Python-world"
# Searching
print(clean_s.find("Python"))# Returns 6 (the starting index of 'Python')
print(clean_s.count("o")) # Returns 3 (number of 'o's)5.4 String Operations and Formatting
Basic operators work on strings. `+` concatenates them, and `*` repeats them. However, when you want to build complex strings involving variables, concatenation gets messy. Python provides multiple ways to format strings, with 'f-strings' being the most modern and preferred method.
# Concatenation and Repetition
print("Ha" * 3) # Output: HaHaHa
print("Py" + "thon") # Output: Python
name = "Alice"
age = 20
# 1. The old % formatting (C-style)
print("My name is %s and I am %d" % (name, age))
# 2. The .format() method (Python 3.0+)
print("My name is {} and I am {}".format(name, age))
# 3. f-strings (Python 3.6+ - The BEST way)
# Prefix the string with 'f' and put variables directly in {}
print(f"My name is {name} and next year I will be {age + 1}")5.5 Escape Characters and Raw Strings
To insert characters that are illegal in a string (like a quote inside a quote, or a newline), you use an escape character: a backslash `\`.
If you are dealing with paths (especially on Windows), backslashes can cause problems. You can prefix the string with `r` to make it a 'raw string', telling Python to ignore escape sequences.
# Escape characters
print("First Line\nSecond Line")
print("He said, \"Python is awesome!\"")
# Raw string (useful for file paths and regular expressions)
path = r"C:\Users\basant\Desktop"
print(path) # Prints exactly as written, no escape sequence issues