2

I want to find a certain substring inside a string. The string is stored in a list of strings. How can i do it?

2
  • 1
    An example would helpful - what does the data look like, what does the search string look like, and what results do you want? Commented Jun 12, 2009 at 21:38
  • Why is this downvoted? You people are ridiculous! Commented Jun 12, 2009 at 21:44

2 Answers 2

4

So you're searching for all the strings in a list of strings that contain a certain substring? This will do it:

DATA = ['Hello', 'Python', 'World']
SEARCH_STRING = 'n'
print [s for s in DATA if SEARCH_STRING in s]
# Prints ['Python']

Edit at Andrew's suggestion: You should read that list comprehension as "Make a list of all the strings in the list DATA where SEARCH_STRING appears somewhere in the string."

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Very Pythonic! It might be best to explain your example though as a newcomer to Python might not be familiar with list comprehensions.
0

have you looked here it is very simple to do, just do a search in the python documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.