Python Program to Remove Duplicate Characters from a String: Simple Guide

Python Program to Remove Duplicate Characters from a String: Simple Guide

How to Remove Duplicate Characters from a String in Python

In Python, strings are a widely used data structure, and one common operation you might perform is removing duplicate characters from a string. This operation is often useful when you need to clean or simplify data, ensuring that each character appears only once.In this article, we will explore different ways to remove duplicate characters from a string in Python and provide a step-by-step guide along with a working Python code example.

What You Will Learn

  • The purpose of removing duplicate characters in a string.
  • Two methods for achieving this in Python.
  • A detailed Python program example.

Input Format:

  • Input: A string of characters.
  • Output: A new string with all duplicate characters removed, ensuring each character appears only once.

Sample Input and Output

Sample Input:Copy codeFACEFACE Sample Output:Copy codeFACEn

Prerequisite Knowledge:

  • Basic understanding of strings and loops in Python.

Steps to Remove Duplicate Characters from a String

We’ll use a straightforward algorithm to remove duplicate characters from a string. The steps involved are:
  1. Input the string from the user.
  2. Traverse through each character of the string.
  3. Check if the character is already present in the result string.
  4. If the character is already present, skip it.
  5. If the character is not present, add it to the result string.
  6. Finally, output the string with unique characters.

Method 1:

We can achieve this by iterating through the string and checking if the character has been encountered previously. If it has, we skip it; otherwise, we add it to the result string.Here’s how you can do it in Python:pythonCopy code# Python program to remove duplicate characters from the given string def remove Duplicate(str1, n):    # Used as an index in the modified string    index = 0        # Traversing through all characters    for i in range(0, n):        # Checking if a character is repeated        for j in range(0, i):            if str1[i] == str1[j]:                break                # If the character is not repeated, add it to the result        if j == i:            str1[index] = str1[i]            index += 1     # Join the result list to form the final string    return “”.join(str1[:index]) # Main function to run the programstr1 = input(“Enter the string: “)n = Len(str1)print(remove Duplicate(list(str1), n)) 

Explanation:

  1. Input the String: We first take the input string from the user.
  2. Traverse Through the String: The outer loop iterates through each character, while the inner loop checks if the current character has appeared before in the string.
  3. Check for Duplicates: If the character is already present, we break the loop and skip adding that character to the result.
  4. Build the Result: If the character is not a duplicate, it’s added to the result string.
  5. Output the Final String: After traversing all characters, we join the list and return the result as a string.

Sample Input and Output

Input:cCopy codeEnter the string: programming Output:Copy codeProgramming 

conclusion:

Removing duplicate characters from a string is an essential operation in many Python applications, especially when cleaning up data. The methods we’ve discussed here—using a simple loop and checks—allow you to efficiently remove duplicates and work with strings more effectively. Python Program to Remove Duplicate Characters from a String
c