-3

I want to write a method which i can use with strings same like we use "word".isupper return False, "word".islower return True, so I want to define a class and want to use like "word".isspecial()

so if i have a list of special characters :

special=["&","*",")("]

and I want that if I have a word I can check like ")(".method() and it return True as we do with "string".islower() which return True

I know I can do this with simple loop and in operator but I want to know how I can do like ")(".method()

What I tried is buggy:

class Lower:

    def check(self,x):
        for i in lower:
            if x==i:
                return True

check1=Lower()

check1.check()

print(check1.check(special,")("))
Matthias
  • 4,481
  • 12
  • 45
  • 84

1 Answers1

0

You can write a function to do what you want like this:

import string

special_set = set(string.punctuation)

def isspecial(s):
    return all(c in special_set for c in s)

print(isspecial(")("))
print(isspecial(")(*)(&)(*&)*&"))
print(isspecial(")(*)(&)(*&)*&x"))

This will output, as expected:

True
True
False

Note I've used string.punctuation to build the set of "special characters". You can define this set by hand if you like.

Really, this is the Pythonic way to do what you want - there's no reason to have to have a method doing this. If you really insist, you can look at this question for more information, but beware it will become messy.

Izaak van Dongen
  • 2,450
  • 13
  • 23