firebanner64
firebanner64

Reputation: 25

Is there anyway I can check whether part of a string in python is somewhere in another string? (beginner)

Let's say we have a string 'abc' and another string 'bcd'. If I do 'abc' in 'bcd' it will return false. I want to say 'if there is a character of 'abc' in the string 'bcd' than return true. (python) edit: thank you for the spelling changes. It makes me feel dumb. They were typos though.

I have tried iterating through the string using for loops, but this is clunky and I am assuming it is not good practice. Anyway I couldn't make it flexible enough for my needs.

 import random
 symb1 = random.choice('abc@') # I am trying to test if it chose AL one 
 # symbol
 symb2 = random.choice('abc!')
 mystring = (symb1+symb2) #lets say mystring is 'a!'
 if mystring in '@!' # I want to test here somehow if part of mystring is 
 # in @!

I want it to output true, and the output is false. I understand why, I just need help creating a way to test for the symbol in mystring

Upvotes: 0

Views: 92

Answers (4)

Rafael Quintela
Rafael Quintela

Reputation: 2208

You could use list comprehensions. Example;

>>> a = 'abc'
>>> b = 'bcd'
>>> [letter for letter in a if letter in b]  # list comprehensions
['b', 'c']
>>> any(letter for letter in a if letter in b)  # generator expression
True

as mentioned by @asikorski; change to use generator expression so the loop stops on the first match.

Upvotes: 1

asikorski
asikorski

Reputation: 922

There are few solutions:

  • the best one (both effective and pythonic):
if any(char in 'bcd' for char in 'abc'):
    ...

Uses generator + built-in any. It does not have to create a list with list comprehension and doesn't waste time to gather all the letters that are in both of strings, so it's memory effective.

  • the boring one (~ C style):
def check_letters(word1, word2):
    for char in word1:
        if char in word2:
            return True
    reeturn False

if check_letters('abc', 'bcd'):
    ...

Quite obvious.

  • the fancy one:
if set(list('abc')) & set(list('bcd')):
    ...

This one uses some tricks. list converts string to a list of letters, set creates a set of letters from the list. Then intersetion of two sets is created with & operator and if's condition evaluates to True if there's any element in the intersection. It's not very effective, though; it has to create two lists and two sets.

Upvotes: 0

firebanner64
firebanner64

Reputation: 25

Okay the comprehensions made more sense but I decided to do a more expanded for loop. I found a way to make it less terrible. I'm just teaching python to a friend and I want him to be able to read the code more easily. here's the section of code in the program.

def punc():
    characters = int(input('How long would you like your password to be? : '))
    passlist = []
    for x in range(characters):
        add = random.choice(string.ascii_letters + '@@@@@$$$$$~~~~!!!!!?????')
        passlist.append(add)
    l = []
    for x in passlist:
        if x in ['@','$','~','!','?']:
            l.append(0)
    if len(l) == 0:
        punc()
    else:
        print(''.join(passlist))

Upvotes: 0

Carcigenicate
Carcigenicate

Reputation: 45745

Iterate one of the Strings while doing in checks:

any(c in "@!" for c in mystring)

"Is there any c from mystring in '@!'?"

Upvotes: 2

Related Questions