• Register
1 vote
595 views

Problem:

Hello Kodlogs,

I am sure you guys are doing well. I just entered the world of Python programming. I’ve got a problem to solve that says, “python check if string contains only certain characters“ I tried several methods. Unfortunately, none of them worked for me. Could you please write a program that can check if a string contains only certain characters?

Thanks in advance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Let say you want to check if a string contains any special characters. You may make a regular expression of a regex object of special characters and pass the string through the search method. If any special characters found it encounters an error immediately.

import re 
def func(string): 
    regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]') 
    if(regex.search(string) == None): 
        print("This String is fine") 
    else: 
        print("Error: Special character found") 
      
if __name__ == '__main__' : 
      
    string = "ThisI$Gavin"
    func(string) 

Dive into the code snippet above and let me know if you still have any questions.

Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 58 views
58 views
Problem: I was trying to check if string contains special characters using a python program. To check this I wrote this code: import re # Function checks if the string # contains any special character def check(string): regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]' ... line 11, in check if(search(string) == None): NameError: name 'search' is not defined How can I solve this problem can anyone help?
asked Jun 10, 2020 samhaz 5k points
1 vote
3 answers 57 views
57 views
Problem: I was trying to check if string contains substring from list using python. So I wrote this code which gives me wrong results. The code I wrote is: mainstring = "Allinall" substring = "all" if substring is mainstring: print ("String Found") else: print (" ... the answer is not right. What is wrong in my code. Cam anyone explain and how can I check if string contains substring from list.
asked Apr 22, 2020 samhaz 5k points
0 votes
1 answer 19 views
19 views
Problem: I've got some Python code that runs through a list of strings and converts them to integers or floating point numbers if possible. Doing this for integers is pretty easy if element.isdigit(): newelement = int(element) Floating point numbers are more difficult. Right now I'm using partition('.') to split the string and checking to make sure that one or both sides are digits.
asked Feb 21 Mashhoodch 9.9k points
0 votes
1 answer 45 views
45 views
Problem: Hello Kodlogs, I am a beginner to python and I am having a doubt that how to check if the character is present in the string or not though, I know one method but wondering if any other method is available or not (in operator ) this is the method I am using ... ) print("Check if "+string+" contains "+subchar1+":") print(subchar1 in string) Waiting for a few more method that can help me.
asked Jun 5, 2020 Gavin 15.3k points
0 votes
1 answer 20 views
20 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It&rsquo;s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16, 2020 LizzyM 6.1k points
0 votes
1 answer 7 views
7 views
Problem: How can I check if a string contains another substring in javascript?Generally I would expect a method String.contains(), but there doesn't seem to be one.
asked Feb 12 sasha 13.2k points
0 votes
1 answer 6 views
6 views
Problem: Hi my friends i am begginer and i want to know how i do that please explain to me with example if you dont explain with example no explain please thank you
asked Feb 9 ahmedadel9090 2.6k points
0 votes
1 answer 34 views
34 views
I am new to this. I just want to know how to do that?
asked Nov 17, 2020 TeamScript 13.5k points
0 votes
1 answer 19 views
19 views
Problem: I am trying to count the occurrences of each letter of a word word = input("Enter a word") Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',' ... ): print(word.count(Alphabet[i])) This currently outputs the number of times each letter occurs including the ones that don't. How do I list the letters vertically with the frequency alongside
asked Jan 22 Mashhoodch 9.9k points
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