Study guide
4.4 String Transformation and Formatting Methods
OpenEDG PCAP — Certified Associate in Python Programming Exam
Track your progress as you read
Finished sections get a check mark.
1 Chapter 1: The PCAP-31-03 Exam Blueprint and Test-Day Logistics
1.1 The PCAP-31-03 Blueprint, Scoring, and Exam-Day Logistics
2 Chapter 2: Modules, Packages, and the Standard Library
2.1 Module Imports and Namespace Mechanics 2.2 Module Construction and Execution 2.3 Package Hierarchy and Navigation 2.4 Standard Library Modules and Pip
3 Chapter 3: Exceptions and Error Handling
3.1 Exception Control Flow and Syntax 3.2 The Built-in Exception Hierarchy 3.3 Raising Exceptions and Assertions 3.4 Custom Exceptions and Exception Chaining
4 Chapter 4: Strings, Encodings, and Text Processing
4.1 Character Encodings and String Immutability 4.2 String Indexing, Slicing, and Comparison 4.3 String Inspection and Classification Methods 4.4 String Transformation and Formatting Methods 4.5 Iterating, Sorting, and Comparing Strings Against Other Types
5 Chapter 5: Object-Oriented Programming Fundamentals
5.1 Classes, Instances, and Attributes 5.2 Encapsulation, Privacy, and Name Mangling 5.3 Dunder Methods and Object Lifecycle 5.4 Discovering Class Structure: __name__, __module__, __bases__, and Identity
6 Chapter 6: Advanced OOP, Inheritance, and Metaprogramming
6.1 Single Inheritance, Method Overriding, and Polymorphism 6.2 Multiple Inheritance and Method Resolution Order (MRO) 6.3 Class Methods, Static Methods, and Properties
7 Chapter 7: Functional Python, Comprehensions, and Closures
7.1 List, Dictionary, and Set Comprehensions 7.2 Lambda Expressions and Higher-Order Functions 7.3 Variable Scopes, the LEGB Rule, and Closures
8 Chapter 8: Iterators, Generators, and File I/O
8.1 Iterators and the Iteration Protocol 8.2 Generator Functions and Generator Expressions 8.3 File Input/Output and Context Managers 8.4 Predefined Streams, Handles vs. Streams, and errno Diagnostics
4.4 String Transformation and Formatting Methods
Key Takeaways Explain This Topic AI
Case transformation methods ( lower() , upper() , capitalize() , title() , swapcase() ) return new string copies with altered character casing according to Unicode rules.
strip() , lstrip() , and rstrip() peel characters from string boundaries by treating their argument as a set of individual characters to remove, rather than as a fixed prefix or suffix substring.
split() with default sep=None groups consecutive whitespace runs and strips boundary whitespace, whereas explicit delimiters ( sep=',' ) treat every delimiter strictly, preserving empty strings between adjacent delimiters.
str.join(iterable) concatenates sequence items using the caller string as a delimiter; if any element in the iterable is not a str , Python raises a TypeError .
replace(old, new[, count]) performs substring substitution, and alignment methods ( center() , ljust() , rjust() , zfill() ) format strings to exact column widths.
Last updated: August 2026
String Transformation and Formatting Methods
String transformation methods in Python produce new string objects formatted or restructured according to specific rules. Because strings are immutable, these methods never alter the original string in-place; they always return a fresh copy.
- 1. Case Transformation Methods
- Python provides five primary methods for altering character case: s = "pYtHoN pRoGrAmMiNg 101"
- # 1. upper(): All cased letters to uppercase
- print(s.upper()) # 'PYTHON PROGRAMMING 101'
- # 2. lower(): All cased letters to lowercase
- print(s.lower()) # 'python programming 101'
- # 3. capitalize(): First character uppercase, ALL remaining characters lowercase
- print(s.capitalize()) # 'Python programming 101'
- # 4. title(): First letter of every word uppercase, remainder lowercase
- print(s.title()) # 'Python Programming 101'
- # 5. swapcase(): Inverts all casing
- print("PyThOn".swapcase()) # 'pYtHoN'
- Special Case Behaviors
Non-alphabetic characters (numbers, spaces, punctuation) are unaffected.
capitalize() forces every character after index 0 to lowercase , even if they were originally capitalized.
title() uses word boundary definitions that can produce non-intuitive results with apostrophes (e.g., "they're".title() becomes "They'Re" ).
2. Whitespace and Character Stripping
The strip family removes characters from the leading and trailing ends of a string:
strip([chars]) : Strips characters from both ends.
lstrip([chars]) : Strips characters from the left (leading) end only.
rstrip([chars]) : Strips characters from the right (trailing) end only.
Default Stripping (Whitespace)
When called without arguments (or with None ), these methods strip all ASCII/Unicode whitespace characters (spaces, \t , \n , \r , \f , \v ): raw_input = " \t\n User Input \r\n "
- print(repr(raw_input.strip())) # 'User Input'
- print(repr(raw_input.lstrip())) # 'User Input \r\n '
- print(repr(raw_input.rstrip())) # ' \t\n User Input'
- Custom Character Set Stripping Mechanics
A common misconception on the PCAP exam is believing that strip("abc") removes the word "abc" . In Python, the argument to strip() is treated as a set of characters to peel off : # The characters 'w', '.', 'c', 'o', 'm' are peeled off in any order
- url = "www.example.com"
- print(url.strip("w.moc")) # 'example'
- # Stripping stops at the first character not present in the set
- word = "spams"
- print(word.strip("mps")) # 'a' ('s' and 'p' removed from left; 's' and 'm' from right)
- text = "banana"
- print(text.strip("ab")) # 'nan' (stops at 'n')
- 3. Splitting and Partitioning Strings
Python offers several methods to divide strings into structured lists or tuples.
str.split(sep=None, maxsplit=-1)
The behavior of split() changes fundamentally depending on whether sep is omitted ( None ) or explicitly specified: +-----------------------+----------------------------------+----------------------------------+
- | Feature | sep=None (Default Whitespace) | sep=',' (Explicit Delimiter) |
- +-----------------------+----------------------------------+----------------------------------+
- | Delimiter | Any run of whitespace | Exact string match only |
- | Consecutive Delimiters| Grouped together as ONE split | Preserved (produces empty strings)|
- | Boundary Whitespace | Automatically stripped | Produces empty strings at bounds |
+-----------------------+----------------------------------+----------------------------------+ # Default whitespace splitting
- data = " alpha beta gamma "
- print(data.split()) # ['alpha', 'beta', 'gamma']
- # Explicit delimiter splitting
- csv_row = ",alpha,,beta,gamma,"
- print(csv_row.split(",")) # ['', 'alpha', '', 'beta', 'gamma', '']
- Limiting Splits with maxsplit and rsplit()
maxsplit sets the maximum number of splits performed; the resulting list contains at most maxsplit + 1 elements.
rsplit() behaves identically to split() , but scans from right to left when maxsplit is specified. s = "2026-08-25-12-00-00"
- # Split from left: at most 2 splits
- print(s.split("-", 2)) # ['2026', '08', '25-12-00-00']
- # Split from right: at most 2 splits
- print(s.rsplit("-", 2)) # ['2026-08-25-12', '00', '00']
- str.splitlines([keepends])
Splits a string at line boundaries ( \n , \r\n , \r , \v , \f ). If keepends=True , line-break characters are preserved in the list elements: multiline = "Line 1\nLine 2\r\nLine 3"
- print(multiline.splitlines()) # ['Line 1', 'Line 2', 'Line 3']
- print(multiline.splitlines(keepends=True))# ['Line 1\n', 'Line 2\r\n', 'Line 3']
- str.partition(sep) and str.rpartition(sep)
Splits a string at the first (or last ) occurrence of sep and returns a 3-tuple: (head, sep, tail) : email = "user@domain.com"
print(email.partition("@")) # ('user', '@', 'domain.com')
# If separator is not found:
- print(email.partition("#")) # ('user@domain.com', '', '')
- print(email.rpartition("#")) # ('', '', 'user@domain.com')
- 4. Joining Sequences: str.join()
The join() method is the inverse of split() . It concatenates the elements of an iterable using the caller string as a separator between each element: separator.join ( iterable ) \text{separator.join}(\text{iterable}) separator.join ( iterable ) words = ["Python", "is", "powerful"]
- print(" ".join(words)) # "Python is powerful"
- print("---".join(words)) # "Python---is---powerful"
- print("".join(words)) # "Pythonispowerful"
- The TypeError Trap with Non-Strings
join() requires every element in the iterable to be a string . If any element is an integer, float, boolean, or None , Python raises a TypeError : numbers = [1, 2, 3, 4]
# This raises TypeError: sequence item 0: expected str instance, int found
try:
result = ",".join(numbers)
except TypeError as err:
print(f"Caught error: {err}")
# Correct Pythonic approach using generator expression or map():
- print(",".join(str(n) for n in numbers)) # '1,2,3,4'
- print(",".join(map(str, numbers))) # '1,2,3,4'
- 5. Substring Replacement: replace()
The replace() method returns a new string with occurrences of old replaced by new : s.replace ( old , new [ , count ] ) \text{s.replace}(\text{old}, \text{new}[, \text{count}]) s.replace ( old , new [ , count ])
count (optional) : Limits the number of replacements performed from left to right.
Replacements are non-overlapping. phrase = "one potato, two potato, three potato"
- # Replace all occurrences
- print(phrase.replace("potato", "tomato"))
- # 'one tomato, two tomato, three tomato'
- # Limit replacements to first 2
- print(phrase.replace("potato", "tomato", 2))
- # 'one tomato, two tomato, three potato'
- # If old substring is absent, original string is returned unchanged
- print(phrase.replace("carrot", "onion"))
- # 'one potato, two potato, three potato'
- 6. Alignment and Padding: center(), ljust(), rjust(), and zfill()
- Python provides methods to format strings to exact column widths: title = "PCAP"
- # center(width[, fillchar]): Center within field of width
- print(title.center(10)) # ' PCAP '
- print(title.center(10, "*")) # '***PCAP***'
- # ljust(width[, fillchar]): Left-align, pad right
- print(title.ljust(10, "-")) # 'PCAP------'
- # rjust(width[, fillchar]): Right-align, pad left
- print(title.rjust(10, "-")) # '------PCAP'
- Zero Padding: str.zfill(width)
The zfill() method pads a numeric string with ASCII '0' digits on the left until the specified width is reached.
Sign-Aware Padding: If the string begins with a leading sign character ( '+' or '-' ), zfill() inserts the zeros after the sign : print("42".zfill(5)) # '00042'
- print("-42".zfill(5)) # '-0042' (sign preserved at index 0)
- print("+7".zfill(4)) # '+007'
- # If width <= len(s), string is returned unaltered
- print("12345".zfill(3)) # '12345'