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)
Interview Tip: Immutability of strings is a very common interview question. Be prepared to explain what it means and why Python strings are designed this way.
2. String Indexing and Slicing
Strings are sequences, meaning their characters are ordered. You can access individual characters or parts of a string using indexing and slicing.
Indexing: Accessing Single Characters
- Positive Indexing: Starts from `0` for the first character.
- Negative Indexing: Starts from `-1` for the last character, `-2` for the second to last, and so on.
my_string = "UdaanPath"
# Indices: 0 1 2 3 4 5 6 7 8
# Negative: -9-8-7-6-5-4-3-2-1
print(f"First character (index 0): {my_string[0]}")
print(f"Fourth character (index 3): {my_string[3]}")
print(f"Last character (index -1): {my_string[-1]}")
print(f"Second to last (index -2): {my_string[-2]}")
First character (index 0): U
Fourth character (index 3): a
Last character (index -1): h
Second to last (index -2): t
Slicing: Extracting Substrings
Syntax: `string[start:end:step]`
- `start`: The starting index (inclusive). Default is 0.
- `end`: The ending index (exclusive). The character at this index is not included. Default is end of string.
- `step`: The step size (how many characters to jump). Default is 1.
sentence = "Python Programming is Powerful"
print(f"First 6 chars: {sentence[0:6]}") # or sentence[:6]
print(f"Chars from index 11: {sentence[11:]}") # 'Programming is Powerful'
print(f"From 7 to 17: {sentence[7:18]}") # 'Programming'
print(f"Last 8 chars: {sentence[-8:]}") # 'Powerful'
print(f"Every second char: {sentence[::2]}") # 'Pto rgamn s oerl'
print(f"Reversed string: {sentence[::-1]}") # 'lufrewoP si gnimmargorP nohtyP'
First 6 chars: Python
Chars from index 11: Programming is Powerful
From 7 to 17: Programming
Last 8 chars: Powerful
Every second char: Pto rgamn s oerl
Reversed string: lufrewoP si gnimmargorP nohtyP
Concatenation (`+`) and Repetition (`*`)
- `+`: Joins two or more strings together.
- `*`: Repeats a string a specified number of times.
greet = "Hello, "
name = "UdaanPath"
full_greeting = greet + name + "!"
print(f"Concatenation: {full_greeting}")
excited_message = "Great! " * 3
print(f"Repetition: {excited_message}")
Concatenation: Hello, UdaanPath!
Repetition: Great! Great! Great!
3. Essential String Methods
Python's str type comes with a rich set of built-in methods for common text operations. Remember, these methods return new strings due to immutability.
Case Conversion & Length:
len(string): (Built-in function, not a method) Returns the length of the string.
.upper(): Converts all characters to uppercase.
.lower(): Converts all characters to lowercase.
.capitalize(): Converts the first character to uppercase, and the rest to lowercase.
.title(): Converts the first character of each word to uppercase.
text = "learning python at udaanpath"
print(f"Length: {len(text)}")
print(f"Uppercase: {text.upper()}")
print(f"Lowercase: {text.lower()}")
print(f"Capitalized: {text.capitalize()}")
print(f"Title Case: {text.title()}")
Length: 28
Uppercase: LEARNING PYTHON AT UDAANPATH
Lowercase: learning python at udaanpath
Capitalized: Learning python at udaanpath
Title Case: Learning Python At Udaanpath
Whitespace Removal:
.strip(): Removes leading and trailing whitespace (spaces, tabs, newlines).
.lstrip(): Removes leading (left) whitespace.
.rstrip(): Removes trailing (right) whitespace.
padded_text = " Hello World! \n"
print(f"Original: '{padded_text}'")
print(f"Stripped: '{padded_text.strip()}'")
print(f"Left stripped: '{padded_text.lstrip()}'")
print(f"Right stripped: '{padded_text.rstrip()}'")
Original: ' Hello World!
'
Stripped: 'Hello World!'
Left stripped: 'Hello World!
'
Right stripped: ' Hello World!'
Replacing and Finding:
.replace(old, new): Replaces all occurrences of `old` substring with `new` substring.
.find(substring): Returns the lowest index where the substring is found. Returns `-1` if not found.
.index(substring): Similar to `find()`, but raises a `ValueError` if the substring is not found.
sentence = "UdaanPath makes coding fun. Coding is powerful."
new_sentence = sentence.replace("Coding", "Programming")
print(f"Replaced: {new_sentence}")
first_index = sentence.find("coding") # Case-sensitive
print(f"Index of 'coding': {first_index}") # returns -1
first_index_case_insensitive = sentence.lower().find("coding")
print(f"Index of 'coding' (case-insensitive): {first_index_case_insensitive}") # returns 14
try:
sentence.index("nonexistent")
except ValueError as e:
print(f"Error using .index(): {e}")
Replaced: UdaanPath makes Programming fun. Programming is powerful.
Index of 'coding': -1
Index of 'coding' (case-insensitive): 14
Error using .index(): substring not found
Splitting and Joining: Extremely Important!
.split(separator): Splits the string into a list of strings using the specified `separator` (default is any whitespace).
separator.join(list_of_strings): Joins elements of an iterable (like a list) into a single string, using the string calling the method as the `separator`.
data_line = "Name:Alice,Age:30,City:New York"
parts = data_line.split(",")
print(f"Split by comma: {parts}") # ['Name:Alice', 'Age:30', 'City:New York']
words = "Python Programming is Awesome".split(" ")
print(f"Split by space: {words}") # ['Python', 'Programming', 'is', 'Awesome']
# Joining words back with a dash
joined_text = "-".join(words)
print(f"Joined with dash: {joined_text}") # Python-Programming-is-Awesome
# Joining with an empty string for no separator
no_sep_join = "".join(['U', 'd', 'a', 'a', 'n', 'P', 'a', 't', 'h'])
print(f"Joined with no separator: {no_sep_join}")
Split by comma: ['Name:Alice', 'Age:30', 'City:New York']
Split by space: ['Python', 'Programming', 'is', 'Awesome']
Joined with dash: Python-Programming-is-Awesome
Joined with no separator: UdaanPath
Interview Tip: .split() and .join() are frequently used and tested! Understand their inverse relationship and how to use them effectively for parsing and constructing strings.
Checking Content:
.startswith(prefix), .endswith(suffix): Checks if string begins/ends with a specified substring.
.isdigit(): Returns `True` if all characters are digits and there is at least one character.
.isalpha(): Returns `True` if all characters are alphabetic and there is at least one character.
.isalnum(): Returns `True` if all characters are alphanumeric (letters or numbers).
.isspace(): Returns `True` if all characters are whitespace.
file_name = "report.pdf"
user_input = "12345"
mixed_input = "Python3"
print(f"'{file_name}' ends with '.pdf': {file_name.endswith('.pdf')}")
print(f"'{user_input}' is digit: {user_input.isdigit()}")
print(f"'{mixed_input}' is alphanumeric: {mixed_input.isalnum()}")
'report.pdf' ends with '.pdf': True
'12345' is digit: True
'Python3' is alphanumeric: True
4. String Formatting: Presenting Your Data Beautifully
Displaying variables and expressions within strings is a common task. Python offers several ways to format strings, with f-strings being the most modern and recommended.
F-strings (Formatted String Literals) - The Modern Way
Introduced in Python 3.6, f-strings provide a concise and readable way to embed expressions inside string literals. Just prefix the string with an `f` or `F`.
name = "Alice"
age = 30
price = 199.99
# Basic embedding
message = f"Hello, {name}! You are {age} years old."
print(message)
# Embed expressions
print(f"Next year, {name} will be {age + 1} years old.")
# Formatting numbers
tax_rate = 0.08
total_cost = price * (1 + tax_rate)
print(f"Product price: ${price:.2f}") # .2f for 2 decimal places
print(f"Total cost with tax: ${total_cost:.2f}")
# Alignment and padding
item = "Laptop"
quantity = 2
print(f"{item:<10} | {quantity:>5}") # Left align item, right align quantity
print(f"{'Mouse':<10} | {1:>5}")
Hello, Alice! You are 30 years old.
Next year, Alice will be 31 years old.
Product price: $199.99
Total cost with tax: $215.99
Laptop | 2
Mouse | 1
Escape Sequences and Raw Strings:
- Escape Sequences: Special characters like newlines (`\n`), tabs (`\t`), or literal quotes within a string are represented using a backslash (`\`).
- Raw Strings (`r"..."`): Prefixing a string literal with `r` treats backslashes as literal characters, ignoring escape sequences. Useful for file paths or regular expressions.
print("Line 1\nLine 2\twith a tab")
print("She said, \"Hello!\"") # Using escape for double quote
# Raw string for a file path (Windows style)
file_path = r"C:\Users\Udaan\Documents\report.txt"
print(f"File path: {file_path}")
Line 1
Line 2 with a tab
She said, "Hello!"
File path: C:\Users\Udaan\Documents\report.txt