DEV Community

Cover image for String manipulation without in built methods in Python Part 2
Peter Muthama
Peter Muthama

Posted on • Edited on

1

String manipulation without in built methods in Python Part 2

On today's blog, we’ll look at more examples of string manipulation without using any built-in Python methods. The key is to understand the problem and break it down step-by-step.


Problem: Swap Letter Cases

Task

Given a string input_string, write a function that transforms all lowercase letters to uppercase and all uppercase letters to lowercase. If the character is not a letter, leave it unchanged.


Understanding the Problem

What we’re asked to do is swap the case of each letter in the string:

  • a becomes A
  • Z becomes z
  • Non-letter characters remain unchanged

Step-by-Step Breakdown

  • Start by creating an empty string to hold the result:
  store = ''
Enter fullscreen mode Exit fullscreen mode
  • Loop through each character in input_string:
  for char in input_string:
Enter fullscreen mode Exit fullscreen mode
  • Use ASCII values to switch cases:
    • Lowercase to uppercase → ord(char) - 32
    • Uppercase to lowercase → ord(char) + 32

Code Implementation

def solution(input_string):
    store = ''

    for char in input_string:
        if 'a' <= char <= 'z':
            store += chr(ord(char) - 32)
        elif 'A' <= char <= 'Z':
            store += chr(ord(char) + 32)
        else:
            store += char

    return store
Enter fullscreen mode Exit fullscreen mode

Why subtract or add 32?

In the ASCII table, lowercase and uppercase letters are separated by 32. For example, 'A' is 65 and 'a' is 9765 + 32 = 97.


Problem: Replace All Occurrences of a Character

Task

Given a string input_string, return a new string where all occurrences of character c1 are replaced by character c2.

Do not use any built-in string methods like .replace()


Understanding the Problem

We simply need to replace every instance of c1 with c2, and leave the rest of the string unchanged.


Step-by-Step Breakdown

  • Create an empty string to store the result:
  store = ''
Enter fullscreen mode Exit fullscreen mode
  • Loop through each character:
    • If it's equal to c1, append c2 to the store.
    • Otherwise, append the original character.

Code Implementation

def replace_character(input_string, c1, c2):
    store = ''

    for char in input_string:
        if char == c1:
            store += c2
        else:
            store += char

    return store
Enter fullscreen mode Exit fullscreen mode

That’s it for Part 2!
In Part 3, we’ll step into more advanced problems—like counting character frequencies and tracking patterns in strings—all without built-in methods. Stay tuned!


Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)