Making sure only a particular group of characters are in a string

mrQWERTY

Is there any way to make sure that only the characters 'm' 'c' 'b' are in a string without resorting to regex?

For instance, if the user inputs 'm', the program will print 'Major'. If the user inputs 'mc', the program will print 'Major, Critical'.

So I want to make sure that if the user inputs something like 'mca', the program will print 'Not applicable'.

try:
    if 'a' in args.findbugs:
        if len(args.findbugs) > 1:
            print 'findbugs: Not an applicable argument.'
        else:
            print 'FINDBUGS:ALL'
    else:
        if 'm' in args.findbugs:
            print 'FINDBUGS:MAJOR'
        if 'c' in args.findbugs:
            print 'FINDBUGS:CRITICAL'
        if 'b' in args.findbugs:
            print 'FINDBUGS:BLOCKER'
except TypeError:
    print "FINDBUGS: NONE"
All Workers Are Essential

Well, the simplest way from what you've described would be:

some_string = 'mca'
if set(some_string) <= {'m', 'c', 'b'}:
    # The string contains only 'm', 'c', or 'b'.
else:
    # The string 'mca' does not match because of 'a'.

Or, if you intend to require at least m, c, or b:

some_string = 'mca'
if set(some_string) & {'m', 'c', 'b'}:
    # The string contains 'm', 'c', or 'b', so 'mca' will match.

NOTE: As pointed out by bgporter, the set literal notation is not available in Python versions less than 2.7. If support for those is required, use set(('m', 'c', 'b')).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Making sure a string repr of decimal number is always n characters long

From Dev

Find number of occurrences of a particular string within a group of characters WITH A CATCH

From Dev

Making sure string numbers operations are culture independent

From Dev

Check if a String contains particular characters

From Dev

How can I check if a string contains only a particular set of special characters in Javascript?

From Dev

PHP only leave text in between particular characters

From Dev

Negate only a particular string in regex

From Dev

Replace only in particular range string

From Dev

Making sure a path string is a valid java path string

From Java

making sure only an existing user in the table can add a record in PostgreSQL

From Dev

java - making sure only one thread initializes object

From Dev

Will a transaction always commit (making sure file is imported only once)?

From Dev

Making sure that the textbox is only filled with a value from a dropdown.

From Dev

Taking input in a script and making sure there's only one parameter

From Dev

FeathersJS - Making sure only the user that registers gets "users created"

From Dev

Regex to match a set of characters but only if particular characters are not grouped

From Dev

Check if a string contains particular characters in any order

From Dev

How to insert a string between particular characters in Python?

From Dev

How to list characters of a particular frequency in a string

From Dev

How to replace before and after characters of a particular string

From Dev

Find value after a particular set of characters in the string

From Dev

Making form's onsubmit to work only for particular submits

From Dev

Making form's onsubmit to work only for particular submits

From Dev

Making box-plot with only particular rows pandas python

From Dev

Making sure Integer.parseInt(String) is a valid int in java

From Dev

Python : seperate particular string from group of strings

From Dev

How to make sure the Print statement is executed only when the characters are distinct?

From Dev

Group string characters into a given length

From Dev

Group string characters into a given length

Related Related

  1. 1

    Making sure a string repr of decimal number is always n characters long

  2. 2

    Find number of occurrences of a particular string within a group of characters WITH A CATCH

  3. 3

    Making sure string numbers operations are culture independent

  4. 4

    Check if a String contains particular characters

  5. 5

    How can I check if a string contains only a particular set of special characters in Javascript?

  6. 6

    PHP only leave text in between particular characters

  7. 7

    Negate only a particular string in regex

  8. 8

    Replace only in particular range string

  9. 9

    Making sure a path string is a valid java path string

  10. 10

    making sure only an existing user in the table can add a record in PostgreSQL

  11. 11

    java - making sure only one thread initializes object

  12. 12

    Will a transaction always commit (making sure file is imported only once)?

  13. 13

    Making sure that the textbox is only filled with a value from a dropdown.

  14. 14

    Taking input in a script and making sure there's only one parameter

  15. 15

    FeathersJS - Making sure only the user that registers gets "users created"

  16. 16

    Regex to match a set of characters but only if particular characters are not grouped

  17. 17

    Check if a string contains particular characters in any order

  18. 18

    How to insert a string between particular characters in Python?

  19. 19

    How to list characters of a particular frequency in a string

  20. 20

    How to replace before and after characters of a particular string

  21. 21

    Find value after a particular set of characters in the string

  22. 22

    Making form's onsubmit to work only for particular submits

  23. 23

    Making form's onsubmit to work only for particular submits

  24. 24

    Making box-plot with only particular rows pandas python

  25. 25

    Making sure Integer.parseInt(String) is a valid int in java

  26. 26

    Python : seperate particular string from group of strings

  27. 27

    How to make sure the Print statement is executed only when the characters are distinct?

  28. 28

    Group string characters into a given length

  29. 29

    Group string characters into a given length

HotTag

Archive