Advanced String Manipulation Techniques in Python
Splitting Strings with Multiple Delimiters
Problem: Split a string containing various delimiters into tokens. For example: s = 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz' where delimiters include ;, |, ,, and \t.
Approaches:
- Apply
str.split()iteratively for each delimiter. - Use
re.split()with a regular expression for single-pass splitting.
Iterative Splitting with str.split():
def split_multiple(s, delimiters):
tokens = [s]
for d in delimiters:
temp = []
for segment in tokens:
temp.extend(segment.split(d))
tokens = temp
return [token for token in tokens if token]
s = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz"
print(split_multiple(s, ",;|\t"))
Single-Pass Splitting with re.split():
import re
s = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz"
print(re.split(r'[,;|\t]+', s))
Checking String Start or End
Problem: Add executable permissions to files ending with .sh or .py in a directory.
Solution: Use str.endswith() with a tuple of suffixes.
filenames = ["a.c", "b.sh", "d.py", "e.java"]
for name in filenames:
if name.endswith((".sh", ".py")):
# Add executable permissions
pass
Reformatting Date Strings
Problem: Convert date strings from yyyy-mm-dd to mm/dd/yyyy.
Solution: Use re.sub() with capture groups to reorder components.
import re
log = "2016-05-21 10:39:26 status unpacked python3-pip:all 2016-05-23 10:49:26 status half-configured python3"
result = re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', log)
print(result)
Joining Multiple Strings
Problem: Efficiently concatenate a list of strings.
Solution:
- Iterative concatenation with
+(inefficient for large lists). - Use
str.join()for optimal performance.
parts = ["<0112>", "<32>", "<1024x768>", "<60>", "<1>", "<100.0>", "<500.0>"]
print(''.join(parts))
Aligning Strings
Problem: Align text to left, right, or center within fixed-width fields.
Solution:
- Use
str.ljust(),str.rjust(),str.center(). - Use formatting with
format()or f-strings.
data = {"a": 100, "as": 0.01, "wer": 500.0, "cc": 12}
max_key_len = max(len(key) for key in data)
for key, value in data.items():
print(f"{key.ljust(max_key_len)} : {value}")
Removing Unwanted Characters
Problem: Strip extraneous characters (whitespace, control characters, diacritics).
Techniques:
- Trimming:
strip(),lstrip(),rstrip(). - Slicing: Remove fixed-position characters.
- Replacement:
replace()orre.sub(). - Translation:
str.translate()with a mapping table.
Examples:
# Trim whitespace
s = " abc 123 "
print(s.strip())
# Remove specific characters
s = "+++abc---"
print(s.strip('+-'))
# Replace control characters
s = "\tabc\t123\txyz\ropt\r"
print(re.sub(r'[\t\r]', '', s))
# Translate characters
s = "abc123def456xyz"
trans_table = str.maketrans("abcxyz", "xyzabc")
print(s.translate(trans_table))
# Remove diacritics
import unicodedata
def remove_accents(s):
nfkd_form = unicodedata.normalize('NFKD', s)
return ''.join(c for c in nfkd_form if not unicodedata.combining(c))
s = "āáǎà ōóǒò ēéěè īíǐì"
print(remove_accents(s))