String contains any character in group?

后端 未结 4 1094
不思量自难忘°
不思量自难忘° 2020-12-10 16:05

I have a set of characters: \\,/,?,% etc. I also have a string, lets say \"This is my string % my string ?\"

I want to check if any of the characters are present in

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 16:12

    You could use any here.

    >>> string = r"/\?%"
    >>> test = "This is my string % my string ?"
    >>> any(elem in test for elem in string)
    True
    >>> test2 = "Just a test string"
    >>> any(elem in test2 for elem in string)
    False
    

提交回复
热议问题