Strings in Python - Notes
Definition of a String
A string is a sequence of one or more Unicode characters (letters, digits, whitespace, symbols).
Strings can be enclosed in single quotes ('...'), double quotes ("..."), or triple quotes ('''...''' or
"""...""").
Indexing & Slicing
Indexing: Access characters via positions—forward from 0, backward from –1 onwards. Slicing:
str[start:end] to get a substring from start (inclusive) up to end (exclusive). If start > end, slicing
returns an empty string. Out-of-range indices are truncated.
String Operations
Concatenation: Use + to join strings. Example: 'Hello' + 'World' = 'HelloWorld'. Repetition: Use * with
an integer. Example: 'abc' * 2 = 'abcabc'. Membership: Use 'in'/'not in' to check substring presence.
Example: 'a' in 'cat' = True.
Traversing Strings
For loop: for ch in str1: print(ch, end='') While loop: index = 0 while index < len(str1):
print(str1[index], end='') index += 1
String Immutability
Strings in Python are immutable — once created, they cannot be altered. Any change creates a
new string.
Common String Methods
Case conversion: upper(), lower(), capitalize(), title(), swapcase(). Trimming: strip(), lstrip(), rstrip().
Searching/counting: find(), index(), count(). Structure: split(), join(), partition(), replace(). Testing:
isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace(), startswith(), endswith().
Summary Table
Concept Description
String Declaration '...', "...", '''...'''
Indexing str[i], negative indices like -1
Slicing str[start:end]
Concatenation + operator
Repetition * operator
Membership in / not in operators
Traversal for ch in str or while loop
Immutability Strings cannot be changed in-place
Common Methods Case, trim, count, find, split, join, replace, test methods