Home > other >  Removing any content after .com/ in python string
Removing any content after .com/ in python string

Time:04-26

I'm trying to remove content after .com/ in a python string to grab just a domain name. I've tried to use split however it splits the url as so:

'example', '.com/', 'examplestring'

Any idea on how to delete everything after ".com/"? For example, I'd like: example.com/examplestring to become example.com.

Any suggestions are greatly appreciated. Thank you

CodePudding user response:

Not clear what the general case is here but if you don't have the protocol (http:// or http://) you could try

"""example.com/examplestring""".split('/')[0]

if you do have the protocol you can do split on '.com/' but that assumes you all your urls are .com.

CodePudding user response:

First split the string by .com and then join the first part of splits with '.com'.

s = "example.com/examplestring"
splits = s.split('.com')
output = splits[0]   '.com'
print(output)

Output:

'example.com'
  • Related