简体   繁体   中英

How to Restrict Certain Words in Text Using Python

I'm making a program that would reject anything if the word "pass" is being used; be it in uppercase or lowercase letters.

That is if the word pAss or PASS is being used.

It also can't be used in inclusion with other words such as addass or fhsaPasS .

For example, my input is asdapaSS, and then it is rejected.

The condition is "Does not contain the string “pass” in any combination of upper or lower case characters."

The code below is what I have come up with as of now.

input1 = input("Please write a word: ")
lowercase = input1.lower()

if len(lowercase) == "pass":
    print("False")
else:
    print("True")

You can use the in operator :

if "pass" in input1.lower(): 
    print("false")
import re
while True:
  string = input()

  if "pass" not in  re.findall("pass",string.lower()):
    print("True")
    break
  else:
    print("False")

This code uses the regular expressions library to check that there is no "pass" in the lowercase version of the string that has just been inputted. findall is used from the regular expressions library to solve it.

import re
string = input()

if "pass" not in  re.findall("pass",string.lower()):
    print("True")
    break
else:
    print("False")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM