Python Strings
- Defining Strings
In Python, strings can be defined using single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """). Triple quotes enable multi-line string definitions.
text_data = ' '
first_text = 'python'
second_text = "best"
third_text = """hello world"""
multi_line_text = """
hello
world
"""
Triple quotes function similarly to multi-line comments and support line breaks. When assigned to a variable, they become string objects; when not assigned, they serve as multi-line comments.
- Quote Nesting
Single-quoted strings can contain double quotes, and double-quoted strings can contain single quotes. Special characters can be escaped with the backslash (\) to treat them as literal characters.
sample1 = '"python"'
sample2 = "'best'"
sample3 = "\"hello world\""
sample4 = '\'hello world\''
print(sample1)
print(sample2)
print(sample3)
print(sample4)
# Output:
"python"
'best'
"hello world"
'hello world'
- String Concatenation and Repetition
The + operator concatenates string variables or literals, but cannot be used with non-string types. The * operator duplicates strings.
greeting = 'hello'
user_name = "Alex"
print(greeting + "," + "I am " + user_name + "!")
print(greeting * 2)
# Output:
hello, I am Alex!
hellohello
- String Formatting
String formatting allows inserting variable values into strings using placeholders.
Old-style Formatting
The syntax uses % followed by a placeholder type. %s converts variables to strings, %d to integers, and %f to floats.
language = 'python'
message = "Learning: %s" % language
print(message)
class_number = 2
current_year = 2024
message = "In %s, I'm in class %s" % (current_year, class_number)
print(message)
# Output:
Learning: python
In 2024, I'm in class 2
Precision control follows the format "m.n":
- m: controls width (minimum characters)
- .n: controls decimal precision
%5d: Ensures integer is 5 characters wide (e.g., 11 becomes " 11")
%7.2f: Ensures 7 characters total with 2 decimal places (e.g., 11.345 becomes " 11.35")
Modern Formatting (f-strings)
F-strings provide a more readable way to format strings:
language = 'python'
print(f"Learning: {language}")
class_number = 2
current_year = 2024
print(f"In {current_year}, I'm in class {class_number}")
# Output:
Learning: python
In 2024, I'm in class 2
Expression Formatting
Expressions (code with clear results) can be directly embedded in formatted strings:
result = 1 * 2
print(f"1 * 2 equals: {result}")
print("1 * 2 equals: %d" % (1 * 2))
# Output:
1 * 2 equals: 2
1 * 2 equals: 2
- Strings as Data Containers
Strings are containers for characters, capable of storing any number of characters.
String Indexing
Python supports two indexing methods: left-to-right starting at 0, and right-to-left starting at -1. Strings are immutable - they cannot be modified after creation.
text = 'python' # Define a string
print(text) # Print entire string
print(text[0:-1]) # Print first to second-to-last character
print(text[0]) # Print first character
print(text[2:5]) # Print third to fifth character
print(text[2:]) # Print from third character to end
# Output:
python
pytho
p
tho
thon
Common String Operations
- Find index: string.find(substring)
- Replace: string.replace(old, new)
- Split: string.split(separator)
- Trim whitespace: string.strip()
- Trim specific characters: string.strip(characters)
- Count occurrences: string.count(substring)
- Get length: len(string)
phrase = "nothing is impossible"
spaced_phrase = " nothing is impossible "
numbered_phrase = "12nothing is impossible21"
print(phrase.find("is"))
print(phrase.replace("is", "was"))
print(phrase.split(" "))
print(phrase.strip())
print(phrase.strip("12"))
print(phrase.count("o"))
print(len(phrase))
# Output:
8
nothing was impossible
['nothing', 'is', 'impossible']
nothing is impossible
nothing is impossible
2
21
- String Characteristics
- Can only store string data
- Arbitrary length (limited by available memory)
- Supports indexing/subscripting
- Allows duplicate characters
- Immutable (cannot be modified after creation)
- Supports iteration with for loops
- String Comparison
String comparison is based on ASCII values. Characters are compared position by position; if any character has a higher ASCII value, the entire string is considered larger.
print("abc" > "abd")
print("ab" > "a")
print("a" > "A")
print("key2" > "key1")
# Output:
False
True
True
True
- Escape Characters
Escape characters allow special characters to be included in strings:
| Escape Character | Meaning |
|---|---|
| \n | Newline |
| \t | Horizontal tab |
| \r | Carriage return |
| \' | Single quote |
| \" | Double quote |
| \\ | Backslash |
| \ooo | Octal value |
| \xhh | Hexadecimal value |
The backslash (\) can also serve as a line continuation character. Raw strings (prefixed with r) prevent escape character interpretation:
print('he\llo')
print(r'he\llo')
# Output:
he
llo
he\llo