String in Python - Lecture Notes
1. String Basics
What are Strings?
Strings in Python are immutable sequences of Unicode characters. Once created, they
cannot be modified.
# Basic string characteristics
text = "Hello, World!"
print(f"Type: {type(text)}") # <class 'str'>
print(f"Length: {len(text)}") # 13
print(f"Is immutable? Yes - try text[0] = 'J' -> TypeError")
Output
Type: <class 'str'>
Length: 13
Is immutable? Yes - try text[0] = 'J' -> TypeError
2. String Creation and Initialization
# Single quotes
name = 'John Doe'
# Double quotes (preferred for containing apostrophes)
message = "It's a beautiful day"
# Triple quotes for multi-line strings
multi_line = """This is line 1
This is line 2
This is line 3"""
# Using str() constructor
number_str = str(12345) # "12345" convert number (int) to string
float_str = str(3.14159) # "3.14159" convert number (float) to
string
bool_str = str(True) # "True" convert bool (true) to
string
# Empty string
empty = ""
print(f"Name: {name}")
print(f"Message: {message}")
print(f"Multi-line:\n{multi_line}")
Output
Name: John Doe
Message: It's a beautiful day
Multi-line:
This is line 1
This is line 2
This is line 3
3. String Operations
3.1 Concatenation
# Method 1: Using + operator
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(f"Concatenation: {full_name}")
# Method 3: f-strings (Python 3.6+)
age = 30
description = f"{full_name} is {age} years old"
print(f"f-string: {description}")
Output
Concatenation: John Smith
f-string: John Smith is 30 years old
3.2 Repetition
# Using * operator
separator = "-" * 50
print(separator)
# Create pattern
pattern = ".*" * 10
print(f"Pattern: {pattern}")
# Practical example - creating table borders
border = "+" + "---+" * 3
print(border)
Output
--------------------------------------------------
Pattern: .*.*.*.*.*.*.*.*.*.*
+---+---+---+
3.3 Slicing (Indexing)
text = "Python Programming"
# Basic indexing
print(f"First character: {text[0]}") # P
print(f"Last character: {text[-1]}") # g
print(f"Second last: {text[-2]}") # n
# Slicing syntax: [start:stop:step]
print(f"Every 2nd char between 1-9: {text[1:9:2]}")
print(f"First 6 chars: {text[:6]}") # Python
print(f"From index 7: {text[7:]}") # Programming
print(f"Chars 7-18: {text[7:18]}") # Programming
print(f"Every 2nd char: {text[::2]}") # Pto rgamn
print(f"Reverse string: {text[::-1]}") # gnimmargorP nohtyP
Output
First character: P
Last character: g
Second last: n
Every 2nd char between 1-9: yhnP
First 6 chars: Python
From index 7: Programming
Chars 7-18: Programming
Every 2nd char: Pto rgamn
Reverse string: gnimmargorP nohtyP
4. String Methods
4.1 Case Conversion Methods
text = "Python Is AWESOME!"
# Case conversions
print(f"Lower: {[Link]()}") # " python is awesome! "
print(f"Upper: {[Link]()}") # " PYTHON IS AWESOME! "
print(f"Title: {[Link]()}") # " Python Is Awesome! "
print(f"Capitalize: {[Link]()}") # "Python is awesome!"
print(f"Swap case: {[Link]()}") # " pYTHON iS awesome! "
Output
Lower: python is awesome!
Upper: PYTHON IS AWESOME!
Title: Python Is Awesome!
Capitalize: Python is awesome!
Swap case: pYTHON iS awesome!
4.2 Strip Methods
messy_string = "***Hello***World***"
# Removing characters from ends
print(f"Strip '*': {messy_string.strip('*')}") # Hello***World
print(f"Lstrip '*': {messy_string.lstrip('*')}") # Hello***World***
print(f"Rstrip '*': {messy_string.rstrip('*')}") # ***Hello***World
# Whitespace removal
whitespace_text = " \t Hello World \n "
print(f"Strip whitespace: '{whitespace_text.strip()}'")
print(f"Lstrip whitespace: '{whitespace_text.lstrip()}'")
print(f"Rstrip whitespace: '{whitespace_text.rstrip()}'")
Output
Strip '*': Hello***World
Lstrip '*': Hello***World***
Rstrip '*': ***Hello***World
Strip whitespace: 'Hello World'
Lstrip whitespace: 'Hello World \n '
Rstrip whitespace: ' \t Hello World'
4.5 Replace Methods
# Replace method
text = "The cat sat on the mat"
# Simple replace
print(f"Replace 'cat' with 'dog': {[Link]('cat', 'dog')}")
# The dog sat on the mat
# Replace with count limit
print(f"Replace first 2 spaces: {[Link](' ', '_', 2)}")
# The_cat_sat on the mat
# Chain replacements
cleaned = [Link]('cat', 'dog').replace('mat', 'rug')
print(f"Chained replace: {cleaned}")
Output
Replace 'cat' with 'dog': The dog sat on the mat
Replace first 2 spaces: The_cat_sat on the mat
Chained replace: The dog sat on the rug
6. Escape Sequences
# Common escape sequences
print("Newline: Hello\nWorld")
print("Tab: Hello\tWorld")
print("Backslash: C:\\Users\\Name")
print("Single quote: 'Hello'")
print("Double quote: \"Hello\"")
print("Carriage return: Hello\rWorld") # Overwrites Hello
print("Backspace: Hello\bWorld") # Removes 'o'
# Raw strings (ignore escape sequences)
path = r"C:\new_folder\[Link]"
print(f"Raw string: {path}")
Output
Newline: Hello
World
Tab: Hello World
Backslash: C:\Users\Name
Single quote: 'Hello'
Double quote: "Hello"
Carriage return: World
Backspace: HellWorld
Raw string: C:\new_folder\[Link]