CodeSparks Logo

CodeSparks

PYTHON

06
CHAPTER
ACTIVE

Strings in Python

Strings are one of the most important data types in Python, used to represent and manipulate text data. From simple messages to complex text processing, strings are essential for user interfaces, data analysis, web development, and countless other programming tasks.

What are Strings?

DEFINITION
String
A sequence of characters enclosed in quotes, used to represent text data. In Python 3.x, strings are Unicode by default, supporting international characters and emojis.

Python 3.x treats all strings as Unicode, making it easy to work with text in any language. Strings are immutable, meaning once created, they cannot be changed - operations that seem to modify strings actually create new string objects.

Creating Strings

Python provides multiple ways to create strings, each useful for different scenarios and text processing needs.

PYTHON
# Single quotes
name = 'Alice'
message = 'Hello, world!'

# Double quotes
title = "Python Programming"
quote = "She said, 'Hello there!'"

# Triple quotes (multiline strings)
poem = """Roses are red,
Violets are blue,
Python is awesome,
And so are you!"""

multiline = '''This is a
multiline string
that spans several lines.'''

# Raw strings (useful for file paths and regex)
file_path = r"C:UsersAliceDocumentsile.txt"
regex_pattern = r"d+.d+"

# Unicode strings (Python 3.x default)
greeting = "Hello 世界! 🐍"
symbols = "α β γ δ ε"

print(f"Name: {name}")           # Alice
print(f"Quote: {quote}")         # She said, 'Hello there!'
print(f"Greeting: {greeting}")   # Hello 世界! 🐍
print(poem)                      # Prints the multiline poem

String Indexing and Slicing

Strings are sequences, so you can access individual characters by their position (index) or extract substrings using slicing operations.

PYTHON
text = "Python"

# Positive indexing (0-based)
print(text[0])    # 'P' (first character)
print(text[1])    # 'y' (second character)
print(text[5])    # 'n' (last character)

# Negative indexing (from the end)
print(text[-1])   # 'n' (last character)
print(text[-2])   # 'o' (second to last)
print(text[-6])   # 'P' (first character)

# String slicing [start:end:step]
word = "Programming"

print(word[0:4])     # 'Prog' (characters 0 to 3)
print(word[4:])      # 'ramming' (from index 4 to end)
print(word[:4])      # 'Prog' (from start to index 3)
print(word[::2])     # 'Pormig' (every 2nd character)
print(word[::-1])    # 'gnimmargorP' (reverse string)

# Advanced slicing
sentence = "Hello, Python World!"
print(sentence[7:13])    # 'Python' (extract specific word)
print(sentence[::3])     # 'Hl,Ph ol!' (every 3rd character)
print(sentence[-6:-1])   # 'World' (slice from end)
FUN FACT

Python's negative indexing is incredibly useful! text[-1] always gives you the last character, text[-2] the second-to-last, and so on. This makes it easy to work with strings without knowing their exact length.

String Concatenation

Combining strings is a fundamental operation in text processing. Python offers several methods for string concatenation, each with its own advantages.

PYTHON
# Using + operator
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)  # "John Doe"

# Using += for building strings
message = "Hello"
message += " "
message += "World"
print(message)    # "Hello World"

# Using join() for multiple strings (most efficient)
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence)   # "Python is awesome"

# Joining with different separators
fruits = ["apple", "banana", "cherry"]
print(", ".join(fruits))     # "apple, banana, cherry"
print(" | ".join(fruits))    # "apple | banana | cherry"
print("".join(fruits))       # "applebananacherry"

# Multiplying strings
laugh = "ha" * 3
print(laugh)      # "hahaha"

border = "-" * 20
print(border)     # "--------------------"

# Combining different approaches
greeting = "Welcome" + " " + "to " * 2 + "Python!"
print(greeting)   # "Welcome to to Python!"

String Formatting

String formatting allows you to create dynamic strings by inserting variables and expressions. Python 3.6+ introduced f-strings, which are now the preferred method for string formatting.

PYTHON
# F-strings (Python 3.6+) - Recommended
name = "Alice"
age = 30
height = 5.6

# Basic f-string formatting
intro = f"My name is {{name}} and I am {{age}} years old."
print(intro)  # "My name is Alice and I am 30 years old."

# Expressions in f-strings
print(f"Next year I'll be {{age + 1}}")  # "Next year I'll be 31"
print(f"My height is {{height:.1f}} feet")  # "My height is 5.6 feet"

# Formatting numbers
price = 19.99
quantity = 3
total = price * quantity

print(f"Price: ${{price:.2f}}")           # "Price: $19.99"
print(f"Total: ${{total:.2f}}")           # "Total: $59.97"
print(f"Quantity: {{quantity:03d}}")      # "Quantity: 003"

# .format() method (older but still useful)
template = "Hello, {{}}! You have {{}} messages."
message = template.format("Bob", 5)
print(message)  # "Hello, Bob! You have 5 messages."

# Named placeholders
info = "Name: {{name}}, Age: {{age}}".format(name="Charlie", age=25)
print(info)  # "Name: Charlie, Age: 25"

# % formatting (oldest, rarely used now)
old_style = "Hello, %s! You are %d years old." % ("David", 28)
print(old_style)  # "Hello, David! You are 28 years old."

# Advanced f-string formatting
from datetime import datetime
now = datetime.now()
print(f"Current time: {{now:%Y-%m-%d %H:%M:%S}}")  # "Current time: 2024-01-15 10:30:45"

# Debugging with f-strings (Python 3.8+)
x = 10
y = 20
print(f"{{x + y = }}")  # "x + y = 30" (shows both expression and result)

Escape Characters

Escape characters allow you to include special characters in strings that would otherwise be difficult or impossible to type directly.

PYTHON
# Common escape characters
print("Hello
World")        # 
 = newline
print("Hello	World")        # 	 = tab
print("Hello\nWorld")       # \ = literal backslash
print("She said, "Hello!"") # " = double quote
print('It's a beautiful day') # ' = single quote

# More escape characters
print("Bell sound: a")      # a = bell/alert (may not be audible)
print("Backspace: abcd")   #  = backspace
print("Form feed: ")       #  = form feed
print("Carriage return: abc
def") # 
 = carriage return
print("Vertical tab: ")    #  = vertical tab

# Unicode escape sequences
print("A")              # \u = Unicode (4 digits): 'A'
print("U00000041")          # U = Unicode (8 digits): 'A'
print("N{LATIN CAPITAL LETTER A}")  # N = Unicode name: 'A'

# Raw strings ignore escape characters
normal_string = "C:
ew	ext.txt"    # 
 and 	 are interpreted as escapes!
raw_string = r"C:
ew	ext.txt"      # Raw string preserves backslashes
print(f"Normal: {repr(normal_string)}")  # Shows the escape interpretation
print(f"Raw: {raw_string}")              # C:
ew	ext.txt

# Triple quotes can contain quotes without escaping
text = """He said, "Python is great!"
She replied, 'I totally agree!'"""
print(text)

String Properties

Strings have several important properties that make them unique among Python data types. Understanding these properties is crucial for effective string manipulation.

PYTHON
# Strings are immutable
original = "Hello"
# original[0] = "h"  # This would cause an error!

# Instead, create a new string
modified = "h" + original[1:]
print(f"Original: {original}")  # "Hello"
print(f"Modified: {modified}")  # "hello"

# String length
message = "Python Programming"
print(f"Length: {len(message)}")  # 18

# Check if string contains substring
text = "I love Python programming"
print("Python" in text)          # True
print("Java" in text)            # False
print("python" in text)          # False (case-sensitive)
print("python" in text.lower())  # True (case-insensitive check)

# String comparison
word1 = "apple"
word2 = "banana"
print(word1 < word2)             # True (alphabetical order)
print("A" < "a")                 # True (uppercase before lowercase)

# Empty strings
empty1 = ""
empty2 = str()
print(f"Empty string length: {len(empty1)}")  # 0
print(f"Is empty: {not empty1}")               # True

# String iteration
word = "Python"
for char in word:
    print(char, end=" ")  # P y t h o n 
print()

# String repetition and membership
repeated = "abc" * 3
print(repeated)                   # "abcabcabc"
print("ab" in repeated)           # True
print("xyz" not in repeated)      # True

# String memory efficiency
text1 = "Hello"
text2 = "Hello"
print(text1 is text2)            # Often True (string interning)
print(id(text1) == id(text2))    # Often True (same memory location)

Mastering strings is essential for Python programming. Whether you're processing user input, reading files, working with APIs, or building user interfaces, strings are the foundation of text manipulation in Python. In the next lesson, we'll explore string methods that provide powerful tools for text processing and manipulation.