• Register
2 votes
49 views

Problem:

Well, let’s assume I have a string in python and I am willing to remove all the duplicates characters from my string. Suppose my string is “Australia” the program should return “Austrli”

So, my question is simple. How can I remove duplicate characters in a string python?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

You can easily remove the duplicates from a string in a python program. It can be done as an orderly format and disorderly format too. I am putting two different ways in a single program. I think it will help you to understand better. 

from collections import OrderedDict 

def removeDuplicatesDisorderly(mystr): 
    return "".join(set(mystr)) 
  
def removeDuplicatesOrderly(mystr): 
    return "".join(OrderedDict.fromkeys(mystr))  
  
if __name__ == "__main__": 
    mystr = "australia"
    print "Without Order = ",removeDuplicatesDisorderly(mystr) 
    print "With Order = ",removeDuplicatesOrderly(mystr) 

And the output you will get:

Without Order =  ailsrut
With Order =  austrli

This might help you to understand the program easily. Keep coding.

13 9 6
94,260 points

Related questions

0 votes
1 answer 48 views
48 views
I need a C program that will help me to remove duplicate characters in a string.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 19 views
19 views
I require to remove all the special characters, puntuation and and speces from a string?
asked Sep 14, 2020 Daniel Anderson 4k points
0 votes
1 answer 469 views
469 views
Problem: Given a string S, the task is to remove all the duplicates in the given string. Below are the different methods to remove duplicates in a string. How do you find duplicate characters in a given string? How do I delete duplicate words in a cell?
asked Dec 12, 2020 Mashhoodch 13k points
0 votes
1 answer 10 views
10 views
I need to know how to print duplicate characters from string in Java.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 21 views
21 views
Problem: To display them, Java has created a special code that can be put into a string: \". Whenever this code is encountered in a string, it is replaced with a double quotation mark.it is difficult to understand how to remove special characters from string anyone can tell me with example.
asked Nov 3, 2020 Mashhoodch 13k points
0 votes
1 answer 27 views
27 views
I need to remove special characters from a string in Java.
asked Sep 29, 2020 Daniel Anderson 4k points
0 votes
1 answer 25 views
25 views
I have to remove special characters from a string using PHP. Can any one here give me the PHP Script?
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 19 views
19 views
I want to know how to remove all punctuation from a string in python
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 10 views
10 views
Problem: I have tried all of the solutions to remove consecutive duplicate characters in a string python and still not be able to fix the problem.
asked Mar 31 zayed1 61.8k points
0 votes
1 answer 89 views
89 views
Problem: Hello! I am a beginner, and my teacher gave me an assignment in which I have to write a program that takes a string as input from the user. If the string contains any consecutive duplicate characters then my program should remove all the consecutive occurrences of that ... . I am weak in programming and have almost no idea how I can do that. Can anybody here help me to solve this problem?
asked Jan 2 Code Learner 9.9k points