Good Evening All,

I'm doing a small application using python. I want to check the special characters of the ASCII values with the given characters in the text box.
if(string1.rindex!="char(32)" and string2.rindex!="char(32)")
message("You have entered the special characters")
else:
check the values in the database and it follows.

Thanks for helping me.
warm Regards
Srinivas.

Recommended Answers

All 2 Replies

if string1.rindex(chr(32)) and string2.rindex(chr(32)):
      print "You have entered the special characters"

That is how you would write the code however this probably isn't the best way as an exception is thrown if the case comes back false something like a for loop would probably be better

for i in string1:
   if i == ' ':         # ' '   is chr(32)
      print 'special character found'

A quicker way would be to check if a split() results in a list with length greater than 1:

>>> s1 = 'Test string'
>>> s2 = 'Test'
>>> s1.split()
['Test', 'string']
>>> s2.split()
['Test']
>>> len(s1.split())
2
>>> len(s2.split())
1
>>>

On second thought, it'd be easier to just do a direct check like this:

>>> def check4space( s ):
...     """ Checks for spaces in the input string s
...         returns True or False """
...     return ' ' in s
...     
>>> check4space( 'This is a test string' )
True
>>> check4space( 'Foobar' )
False
>>> check4space( '13457890)(*&^%$#@!' )
False
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.