Lecture 4: Strings and Data Types

Python Programming - Unit I

Mutable and Immutable Data Types

Key Concept

In Python, data types are classified as either mutable (can be changed after creation) or immutable (cannot be changed after creation).

Immutable Data Types

Immutable objects cannot be modified after they are created. Any operation that appears to modify them actually creates a new object.

Data Type Description Example
int Integer numbers x = 10
float Floating-point numbers x = 3.14
str Strings x = "Hello"
tuple Ordered sequences x = (1, 2, 3)
frozenset Immutable sets x = frozenset({1, 2, 3})
bool Boolean values x = True

Mutable Data Types

Mutable objects can be modified after they are created without creating a new object.

Data Type Description Example
list Ordered sequences x = [1, 2, 3]
dict Key-value pairs x = {"a": 1, "b": 2}
set Unordered collections x = {1, 2, 3}
bytearray Mutable byte arrays x = bytearray(b"Hello")

Examples Demonstrating Mutability

Immutable Example (String):
# Strings are immutable
text = "Hello"
print(f"Original: {text}")
print(f"ID: {id(text)}")

# This creates a new string, doesn't modify the original
text = text + " World"
print(f"Modified: {text}")
print(f"New ID: {id(text)}")  # Different ID
Mutable Example (List):
# Lists are mutable
numbers = [1, 2, 3]
print(f"Original: {numbers}")
print(f"ID: {id(numbers)}")

# This modifies the original list
numbers.append(4)
print(f"Modified: {numbers}")
print(f"Same ID: {id(numbers)}")  # Same ID

Important Note

When you pass immutable objects to functions, the function receives a copy. When you pass mutable objects, the function receives a reference to the original object.

String Values

Strings in Python are sequences of characters enclosed in single quotes, double quotes, or triple quotes.

Creating Strings

# Single quotes
name = 'John Doe'

# Double quotes
message = "Hello, World!"

# Triple quotes (for multiline strings)
paragraph = """This is a
multiline string that can span
multiple lines."""

# Raw strings (no escape sequences)
path = r"C:\Users\Documents\file.txt"

String Properties

  • Immutable: Cannot be changed after creation
  • Ordered: Characters have specific positions
  • Indexable: Can access individual characters
  • Sliceable: Can extract substrings

String Indexing and Slicing

text = "Python Programming"

# Indexing (0-based)
print(text[0])     # Output: P
print(text[-1])    # Output: g (last character)

# Slicing [start:end:step]
print(text[0:6])           # Output: Python
print(text[7:])            # Output: Programming
print(text[::2])           # Output: Pto rgamn
print(text[::-1])          # Output: gnimmargorP nohtyP (reversed)

String Length

text = "Hello, World!"
print(len(text))  # Output: 13

String Methods

Python provides numerous built-in methods for string manipulation. Since strings are immutable, these methods return new strings rather than modifying the original.

Method Description Example
upper() Converts to uppercase "hello".upper() → "HELLO"
lower() Converts to lowercase "HELLO".lower() → "hello"
title() Title case (first letter capitalized) "hello world".title() → "Hello World"
capitalize() First letter capitalized, rest lowercase "hello WORLD".capitalize() → "Hello world"
strip() Removes whitespace from both ends " hello ".strip() → "hello"
lstrip() Removes whitespace from left end " hello".lstrip() → "hello"
rstrip() Removes whitespace from right end "hello ".rstrip() → "hello"
find() Returns first occurrence index or -1 "hello".find("l") → 2
rfind() Returns last occurrence index or -1 "hello".rfind("l") → 3
index() Similar to find() but raises ValueError "hello".index("l") → 2
count() Counts occurrences of substring "hello".count("l") → 2
replace() Replaces substring "hello".replace("l", "x") → "hexxo"
split() Splits string into list "a b c".split() → ["a", "b", "c"]
join() Joins iterable into string "-".join(["a", "b", "c"]) → "a-b-c"
startswith() Checks if string starts with prefix "hello".startswith("he") → True
endswith() Checks if string ends with suffix "hello".endswith("lo") → True
isalpha() Checks if all characters are alphabetic "hello".isalpha() → True
isdigit() Checks if all characters are digits "123".isdigit() → True
isalnum() Checks if alphanumeric "hello123".isalnum() → True
isspace() Checks if whitespace " ".isspace() → True
isupper() Checks if uppercase "HELLO".isupper() → True
islower() Checks if lowercase "hello".islower() → True
istitle() Checks if title case "Hello World".istitle() → True
center() Centers string in specified width "hello".center(10) → " hello "
ljust() Left justifies string "hello".ljust(10) → "hello "
rjust() Right justifies string "hello".rjust(10) → " hello"
zfill() Pads with zeros on left "42".zfill(5) → "00042"

Examples of String Methods

text = "  Python Programming is Fun!  "

# Case conversion methods
print(text.upper())        # "  PYTHON PROGRAMMING IS FUN!  "
print(text.lower())        # "  python programming is fun!  "
print(text.title())        # "  Python Programming Is Fun!  "
print(text.capitalize())   # "  python programming is fun!  "

# Whitespace methods
print(text.strip())        # "Python Programming is Fun!"
print(text.lstrip())       # "Python Programming is Fun!  "
print(text.rstrip())       # "  Python Programming is Fun!"

# Search methods
print(text.find("Python")) # 2
print(text.count("i"))      # 2
print(text.startswith("  ")) # True
print(text.endswith("!  "))  # True

# Replacement
print(text.replace("Fun", "Awesome")) # "  Python Programming is Awesome!  "

# Splitting and joining
words = text.strip().split()
print(words)  # ["Python", "Programming", "is", "Fun!"]
joined = "-".join(words)
print(joined)  # "Python-Programming-is-Fun!"

String Operators

Python supports several operators for working with strings.

Concatenation Operators

# Addition operator (+) for concatenation
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)  # Output: John Doe

# Multiple concatenation
greeting = "Hello" + ", " + "World" + "!"
print(greeting)  # Output: Hello, World!

Repetition Operator

# Multiplication operator (*) for repetition
pattern = "*" * 10
print(pattern)  # Output: **********

word = "Python "
repeated = word * 3
print(repeated)  # Output: Python Python Python 

Membership Operators

text = "Python Programming"

# in operator
print("Python" in text)      # True
print("Java" in text)        # False

# not in operator
print("Java" not in text)    # True

Comparison Operators

# Lexicographical comparison
str1 = "apple"
str2 = "banana"

print(str1 < str2)   # True (a comes before b)
print(str1 > str2)   # False
print(str1 == str2)  # False
print(str1 != str2)  # True

# Case-sensitive comparison
print("Apple" < "apple")  # True (uppercase comes before lowercase)

Formatting Operators

# Old style formatting (% operator)
name = "John"
age = 25
print("Name: %s, Age: %d" % (name, age))

# f-strings (recommended)
print(f"Name: {name}, Age: {age}")

# format() method
print("Name: {}, Age: {}".format(name, age))

Slicing Operator []

text = "Python Programming"

# Basic slicing
print(text[0:6])     # Python
print(text[7:])      # Programming
print(text[:6])      # Python

# Step slicing
print(text[::2])     # Pto rgamn
print(text[::-1])    # gnimmargorP nohtyP (reversed)

Escape Sequence Characters

Escape sequences are special characters used in strings to represent characters that are difficult to express directly.

Escape Sequence Description Example
\\ Backslash character "C:\\Users\\file.txt"
\' Single quote "It\'s sunny"
\" Double quote 'He said "Hello"'
\n Newline "Line 1\nLine 2"
\t Horizontal tab "Name:\tJohn"
\r Carriage return "Hello\rWorld"
\b Backspace "Hello\bWorld"
\f Form feed "Page 1\fPage 2"
\v Vertical tab "Line 1\vLine 2"
\a Bell/alert "Warning\a"
\ooo Octal value (ooo) "\101" → "A"
\xhh Hexadecimal value (hh) "\x41" → "A"
\N{name} Unicode character by name "\N{GREEK CAPITAL LETTER DELTA}" → "Δ"
\uxxxx Unicode character (16-bit) "\u03A9" → "Ω"
\Uxxxxxxxx Unicode character (32-bit) "\U0001F600" → "😀"

Examples of Escape Sequences

# Quotes in strings
message1 = "He said, \"Hello, World!\""
message2 = 'It\'s a beautiful day'

# Newlines and tabs
address = "John Doe\n123 Main St\tNew York, NY"
print(address)

# Backslashes
file_path = "C:\\Users\\Documents\\file.txt"
print(file_path)

# Unicode characters
greek_letter = "\u03A9"  # Omega symbol
emoji = "\U0001F600"     # Smiling face emoji
print(f"Greek: {greek_letter}, Emoji: {emoji}")

# Raw strings (no escape sequences)
raw_path = r"C:\Users\Documents\file.txt"
print(raw_path)

Tip

Use raw strings (prefix with r) when dealing with file paths or regular expressions to avoid unintended escape sequence processing.

Practice Questions

Question 1: Mutable vs Immutable

What will be the output of the following code? Explain why.

def modify_data(data):
    data.append(4)
    return data

numbers = [1, 2, 3]
result = modify_data(numbers)
print(f"Original: {numbers}")
print(f"Result: {result}")
print(f"Same object: {numbers is result}")

Question 2: String Immutability

Explain why this code doesn't modify the original string:

text = "hello"
text.upper()
print(text)  # What does this print?

Question 3: String Methods

Write a single line of code to transform the string " PYTHON PROGRAMMING " into "Python Programming".

Question 4: String Slicing

Given the string text = "abcdefghij", what will each of these expressions return?

text[2:8]
text[::3]
text[::-1]
text[1:8:2]

Question 5: Escape Sequences

Create a string that prints the following output exactly:

Path: C:\Users\Documents
Message: "Hello, World!"
Status: OK

Question 6: String Operations

What is the output of this code?

word1 = "Python"
word2 = "Programming"
result = word1[0:3] + word2[-4:]
print(result)

Question 7: String Methods Chain

What does this code output?

text = "  hello python world  "
result = text.strip().title().replace(" ", "-")
print(result)

Question 8: String Formatting

Using f-strings, format the following data into the string: "Name: John Doe, Age: 25, City: New York"

name = "John Doe"
age = 25
city = "New York"
# Your f-string here

Question 9: String Validation

Write a function that validates if a string is a valid password with these rules: - At least 8 characters - Contains at least one digit - Contains at least one uppercase letter - Contains at least one lowercase letter

Question 10: String Manipulation

Write a function that counts the number of words in a string. Words are separated by spaces, but multiple spaces should be treated as a single separator.

Solutions

Answer 1: Original: [1, 2, 3, 4], Result: [1, 2, 3, 4], Same object: True. Lists are mutable, so the function modifies the original list.

Answer 2: It prints "hello". String methods return new strings; they don't modify the original. Use text = text.upper() to capture the result.

Answer 3: " PYTHON PROGRAMMING ".strip().title()

Answer 4: "cdefgh", "adg", "jihgfedcba", "bdfh"

Answer 5: print("Path: C:\\Users\\Documents\nMessage: \"Hello, World!\"\nStatus: OK")

Answer 6: "Pything"

Answer 7: "Hello-Python-World"

Answer 8: f"Name: {name}, Age: {age}, City: {city}"

Answer 9:

def validate_password(password):
    if len(password) < 8:
        return False
    has_digit = any(c.isdigit() for c in password)
    has_upper = any(c.isupper() for c in password)
    has_lower = any(c.islower() for c in password)
    return has_digit and has_upper and has_lower

Answer 10:

def count_words(text):
    words = text.strip().split()
    return len(words)