2

I need a command in python which filters the letters in a string but not deleting them for example:

string =raw_input("Enter the string:")
if string.startswith("a") and string.endswith("aaa"):
    print "String accepted"
else: print "String not accepted"
if "ba" in string:
    print "String accepted"
else :
    print "String is not accepted"

What should I add to ban other letters except a and b in the string

The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • 1
    @machineyearning the type is `str`; `string` is a standard library module, so it's probably best not to shadow it, but it's not such a big deal. – jonrsharpe Oct 08 '15 at 11:08
  • @machineyearning: as jonrsharpe has already said, no builtin is being shadowed here – Andrea Corbellini Oct 08 '15 at 11:19
  • `string` is not a class, nor a "builtin module". Builtins are those names that you can find in `__builtins__`. Perhaps what you meant to say is that string is a module from the standard library. – Andrea Corbellini Oct 08 '15 at 11:29

5 Answers5

2

You can simply replace them with the empty string and check whether there's anything left:

string = raw_input("Enter the string:")
if string.replace('a','').replace('b',''):
    print "String not accepted"
else:
    print "String accepted"

The original string string will not be modified.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

You can use a set , convert your string to a set and check if its a subset of a set with only a and b. Example -

s = raw_input("Enter the string:")
validset = set('ab')
if set(s).issubset(validset):
    print "String accepted"
else: 
    print "String not accepted"

Demo -

>>> s = "abbba"
>>> validset = set(['a','b'])
>>> if set(s).issubset(validset):
...     print "String accepted"
... else: print "String not accepted"
... 
String accepted

>>> s = "abbbac"
>>> if set(s).issubset(validset):
...     print "String accepted"
... else: print "String not accepted"
... 
String not accepted

Or as indicated in the comments you can use set.issuperset() instead . Example -

s = raw_input("Enter the string:")
validset = set('ab')
if validset.issuperset(s):
    print "String accepted"
else: 
    print "String not accepted"
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

This is a good use of regular expressions:

import re
my_string = raw_input("Enter the string:")
if re.match('^[ab]+$', my_string):
    print "String accepted"
else :
    print "String is not accepted"

This will match strings which contain only the characters a and b, of non-zero length. If you want to match zero-length strings, use * instead of +.

machine yearning
  • 9,889
  • 5
  • 38
  • 51
0

You could use a regex to search for letters and combinations you want to filter.

^((?![abcd]).)*$ will match things that don't contain a, b, c or d.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Astrogat
  • 1,617
  • 12
  • 24
0

Try something like this. It lets you set multiple letters, symbols, etc

valid_char = ['a', 'b']

def validate(s):
    for char in s:
        if char not in valid_char:
            return False
    return True

if validate(input("Enter the string:")):
    print('Sting Accepted')
else:
    print('Sting not Accepted')
Steven Summers
  • 5,079
  • 2
  • 20
  • 31