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

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,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 24 views
24 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 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 5 views
5 views
Problem: I'm trying to write a method that removes all non alphabetic characters from a Java String[] and then convert the String to an lower case string. I've tried using regular expression to replace the occurence of all non alphabetic characters by "" .However, the output that I am getting is not able to do so
asked 5 days ago Ali 4.4k points
0 votes
1 answer 15 views
15 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.5k 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 31 views
31 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 5.8k points