A string is a sequence of characters. In Python, strings are created by enclosing characters in single quotes ' or double quotes ".
Strings are ordered sequences, which means you can access individual characters using index numbers (starting from 0), or select a range of characters using slicing.
- Positive Indices: Count from the beginning (start at
0). - Negative Indices: Count from the end (start at
-1). - Slicing Syntax:
string[start:end:step]
text = "Hello, World!"
# Indexing
print(text[0]) # Output: H
print(text[-1]) # Output: !
# Slicing
print(text[0:5]) # Output: Hello (excludes index 5)
print(text[7:]) # Output: World! (from index 7 to the end)
print(text[::2]) # Output: Hlo ol! (every second character)
print(text[::-1]) # Output: !dlroW ,olleH (reverses the string)Python strings are immutable, meaning that once a string is created, its characters cannot be changed or replaced.
text = "hello"
# The line below will raise a TypeError:
# text[0] = "H"
# To modify a string, you must create a new one:
text = "H" + text[1:]
print(text) # Output: HelloEscape characters allow you to print special characters that are otherwise difficult or impossible to type in a string:
\n: Newline\t: Tab\\: Backslash\": Double Quote
print("Hello\nWorld") # Prints Hello on one line and World on the nextIf you prefix a string with r or R, Python will ignore all escape characters. This is extremely useful for file paths and regular expressions.
# Normal string (interprets \n as newline)
print("C:\new_folder")
# Raw string (prints exactly what is typed)
print(r"C:\new_folder") # Output: C:\new_folderf-strings do not just embed variable values; they can also evaluate expressions and format numbers.
# Math expressions
print(f"5 + 5 is {5 + 5}") # Output: 5 + 5 is 10
# Float precision formatting
pi = 3.14159265
print(f"Pi to 2 decimal places: {pi:.2f}") # Output: Pi to 2 decimal places: 3.14
# Text alignment
print(f"{'right-aligned':>20}") # Output: right-aligned
# Nested quotes
user = {"name": "Alice"}
print(f"User's name is {user['name']}")Here is a reference list of the most common string methods:
| Method | Description | Example |
|---|---|---|
upper() |
Converts string to uppercase. |
"hello".upper() "HELLO"
|
lower() |
Converts string to lowercase. |
"HELLO".lower() "hello"
|
title() |
Capitalizes the first letter of each word. |
"hello world".title() "Hello World"
|
strip() |
Removes leading and trailing whitespace. |
" hello ".strip() "hello"
|
lstrip() / rstrip()
|
Removes left or right whitespace. |
" hello ".lstrip() "hello "
|
split(sep) |
Splits string into a list using separator. |
"a,b".split(",") ['a', 'b']
|
join(iterable) |
Joins an iterable into a string. |
"-".join(["a", "b"]) "a-b"
|
replace(old, new) |
Replaces occurrences of a substring. |
"abc".replace("b", "x") "axc"
|
find(sub) |
Returns the index of first occurrence. |
"hello".find("l") 2 (returns -1 if not found) |
count(sub) |
Counts occurrences of a substring. |
"hello".count("l") 2
|
startswith(prefix) |
Checks if string starts with prefix. |
"hello".startswith("he") True
|
endswith(suffix) |
Checks if string ends with suffix. |
"hello".endswith("lo") True
|
isdigit() |
Returns True if all characters are digits. |
"123".isdigit() True
|
isalpha() |
Returns True if all characters are alphabetic. |
"abc".isalpha() True
|