[Coding Test] Essential Libraries

EJ JungEJ Jung
2 min read

1. Grammar Tips

1.1. List Comprehensions:

  • Simplify tasks like filtering or transforming strings:

      vowels = [c for c in "hello" if c in "aeiou"]
    

1.2. Efficient String Iteration:

  • Use enumerate() to loop through a string with indices:

      for i, c in enumerate("hello"):
          print(i, c)
    

1.3. Set for Unique Characters:

  • Quickly identify unique characters:

      unique_chars = set("abracadabra")  # {'a', 'b', 'r', 'c', 'd'}
    

2. String Methods (Built-in Functions)

2.1. Basic Operations

  • len(s): Get the length of a string.

  • s[i]: Access the character at index i.

2.2. Case Handling

  • s.lower(), s.upper(): Convert to lowercase/uppercase.

  • s.capitalize(), s.title(): Capitalize the first character or each word.

  • s.swapcase(): Swap lowercase to uppercase and vice versa.

2.3. String Search and Matching

  • s.find(sub): Returns the first occurrence of sub or -1 if not found.

  • s.rfind(sub): Like find, but searches from the end.

  • s.count(sub): Count occurrences of a substring.

2.4. String Manipulation

  • s.strip(), s.lstrip(), s.rstrip(): Remove whitespace or specified characters.

  • s.replace(old, new): Replace all occurrences of old with new.

  • s.split(sep): Split a string into a list of substrings.

  • 'sep'.join(iterable): Join elements of an iterable with a string.

2.5. Validation

  • s.isdigit(), s.isalpha(), s.isalnum(): Check if the string is all digits, letters, or alphanumeric.

  • s.isspace(): Check if the string is only whitespace.

2.6. Slicing and Reversing

  • s[start:end:step]: Slice a string.

  • s[::-1]: Reverse a string.


3. Libraries for String Manipulation

3.1. re (Regular Expressions)

  • Use for pattern matching and advanced text processing.

  • Common methods:

    • re.match(): Match a pattern at the start of a string.

    • re.search(): Search for a pattern anywhere in the string.

    • re.findall(): Find all occurrences of a pattern.

    • re.sub(): Replace matches with a substring.

    • Example:

        import re
        re.findall(r'\d+', 'abc123def456')  # ['123', '456']
      

3.2. collections

  • collections.Counter:

    • Count the frequency of characters or substrings.

    • Example:

        from collections import Counter
        Counter("aabbcc")  # Counter({'a': 2, 'b': 2, 'c': 2})
      
0
Subscribe to my newsletter

Read articles from EJ Jung directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

EJ Jung
EJ Jung