Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

slicing an string after a word in python

So my issue is I have a string/URL:

"https://www.youtube.com/watch?v=FOoMXn3N5ME&list=PL5E1B8DFA8A07A9FA"

And I want to trim the

"https://www.youtube.com/watch?v=FOoMXn3N5ME&list="

Leaving only the playlist Id which is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"PL5E1B8DFA8A07A9FA"

I know I could do something with slicing and indexing like

string = string[0:49]

but for my application the input link/URL will vary in size
and the playlist id or video id will change the length of the string. So how can I trim this string directly after the

list=

Possibly needed resources:
python: 3.8.10
pip: 20.0.2

>Solution :

Instead of trying to slice the string and handling all sorts of potential edge cases, I’d use urllib.parse:

from urllib.parse import *
url = "https://www.youtube.com/watch?v=FOoMXn3N5ME&list=PL5E1B8DFA8A07A9FA"
list_id = parse_qs(urlparse(url).query)['list'][0]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading