2

I wanted to ask if there is any way of finding a sequence of characters from a bigger string in python? For example when working with urls i want to find www.example.com from http://www.example.com/aaa/bbb/ccc. If found, it should return True.

Or is there any other way to do so? Thanks in advance!

1

3 Answers 3

8

Use in to test if one string is a substring of another.

>>> "www.example.com" in "http://www.example.com/aaa/bbb/ccc"
True
Sign up to request clarification or add additional context in comments.

1 Comment

Okay thanks! It was this simple! I was actually trying to use a a.find(b) method which dint work!
1

I'm showing another way to do this

>>> data = "http://www.example.com/aaa/bbb/ccc"
>>> key = "www.example.com"

>>> if key in data:
...     #do your action here

Comments

0

There are number of ways to do it:

>>> a = "http://www.example.com/aaa/bbb/ccc"
>>> b = "www.example.com"
>>> if a.find(b) >=0:            # find return position of string if found else -1
...     print(True)
... 
True
>>> import re
>>> if re.search(b,a):              
...     print(True)
... 
True

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.