Ashim
Ashim

Reputation: 927

How to check if individual character in a string exists in another string for python

Is there any library that allows me to check If all the individual characters in one string exists in another string. When i try to use in what happens is the character has to be a substring. It only works for 1234 and 123. However i want something that checks individual characters. I want a library that gives me the output: string 2 is in string 1 for the following code.

string1 = '1234'
string2 = '24'

if string2 in string1:
    print('string2 is in string1')
else:
    print('string2 is not in string1')

Upvotes: 0

Views: 435

Answers (1)

Austin
Austin

Reputation: 26039

You can use all() with a generator. This returns a true only if all conditions are a true else false:

string1 = '1234'
string2 = '24'

if all(x in string1 for x in string2):
    print('string2 is in string1')
else:
    print('string2 is not in string1')

Or, you can use set's issubset:

set(string2).issubset(string1)

Upvotes: 2

Related Questions