• Register
1 vote
42 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.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

We can easily sort out the special characters from the given string. To do so, we need to compare the special characters (excepts (a-z) & (A-Z)) with the ASCII value. The character whose value does not lie in the range of alphabets, we will remove those characters from our string.

Let’s dig into the program directly.

def removeSpcChars(str): 
    r = "" 
    for i in str:           
        if (i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z'): 
            r += i 
    print(r)  
str = "$My*N;a.me.Is, G'a^vi.n?"
removeSpcChars(str)

And the program should produce an output

MyNameIsGavin

 

13 9 6
94,260 points

Related questions

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 33 views
33 views
Problem Hello Kodlogs, How do I remove all special characters from string php using a function?
asked Nov 3, 2020 hashq 1.1k 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
Problem I need to clean string from unnecessary characters, which are brought by users via UI. For instance the system got the following input from a user: Berry @GSC,accepts.OR1245; I need to get : Berry GSC accepts OR1245;
asked Nov 7, 2020 alexh 2.6k points
0 votes
1 answer 47 views
47 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 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 100 views
100 views
Problem: Hi there! I am newbie and I am want to strip away specific characters from the end of a string. I am programming in C#. I have tried Remove() method as follows: using System; namespace ConsoleApp2 { class Program { static void Main(string[] args) { ... the index of string myself. I want to know that is there any way in C# to remove characters from strings without specifying the index?
asked Nov 27, 2020 Code Learner 9.8k points
0 votes
1 answer 9 views
9 views
I need to know how to print duplicate characters from string in Java.
asked Oct 3, 2020 Daniel Anderson 4k points