0

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.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by jlm699
0
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'
0

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
>>>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.