• Register
2 votes
40 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,240 points

Related questions

0 votes
1 answer 13 views
13 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 20 views
20 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 50 views
50 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 9.1k points
0 votes
1 answer 11 views
11 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 16 views
16 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 9.1k points
0 votes
1 answer 21 views
21 views
I need to remove special characters from a string in Java.
asked Sep 29, 2020 Daniel Anderson 4k points
0 votes
1 answer 24 views
24 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 7 views
7 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: 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 5.8k points
1 vote
1 answer 35 views
35 views
Problem: Hi all, I am a novice in python programming. I got a problem in the textbook exercise I was learning from. It says to remove all special characters from string python. I’ve tried several ways that didn’t work for me. Unfortunately, I removed all the char from my string. So, what is the best practice to do it if it is doable? Thanks for your valuable time. Best regards.
asked Apr 5, 2020 Gavin 15.3k points