Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Advanced String Manipulation Techniques in Python

Tech Aug 12 18

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:

  1. Apply str.split() iteratively for each delimiter.
  2. 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:

  1. Iterative concatenation with + (inefficient for large lists).
  2. 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:

  1. Trimming: strip(), lstrip(), rstrip().
  2. Slicing: Remove fixed-position characters.
  3. Replacement: replace() or re.sub().
  4. 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))

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.