hnvasa
hnvasa

Reputation: 860

Searching a sequence of characters from a string in python

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!

Upvotes: 2

Views: 23281

Answers (3)

Raihan
Raihan

Reputation: 171

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

Upvotes: 1

Hackaholic
Hackaholic

Reputation: 19733

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

Upvotes: 0

Kevin
Kevin

Reputation: 76184

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

>>> "www.example.com" in "http://www.example.com/aaa/bbb/ccc"
True

Upvotes: 8

Related Questions