I wanna split everything comes after = and assigning it into a new variable
example:
https://www.exaple.com/index.php?id=24124
I wanna split whatever comes after = which's in this case 24124 and put it into a new variable.
I wanna split everything comes after = and assigning it into a new variable
example:
https://www.exaple.com/index.php?id=24124
I wanna split whatever comes after = which's in this case 24124 and put it into a new variable.
You can of course split this specific string. rsplit() would be a good choice since you are interested in the rightmost value:
s = "https://www.exaple.com/index.php?id=24124"
rest, n = s.rsplit('=', 1)
# n == '24124'
However, if you are dealing with URLs this is fragile. For example, a url to the same page might look like:
s = "https://www.exaple.com/index.php?id=24124#anchor"
and the above split would return '24124#anchor', which is probably not what you want.
Python includes good url parsing, which you should use if you are dealing with URLS. In this case it's just as simple to get what you want and less fragile:
from urllib.parse import (parse_qs, urlparse)
s = "https://www.exaple.com/index.php?id=24124"
qs = urlparse(s)
parse_qs(qs.query)['id'][0]
# '24124'
id value and properly ignores the fragment starting with #. The reason it is a list is that you can have a url like https://www.exaple.com/index.php?id=24124&id=100, in which case parse_qs(qs.query)['id'] would have two values. It helps understanding this to print parse_qs(qs.query)['id'] and see what that is in various cases.Simply you use .split() and then take the second part only
url = 'https://www.exaple.com/index.php?id=24124'
print(url.split('=')[1])
For your specific case, you could do...
url = "https://www.exaple.com/index.php?id=24124"
id_number = url.split('=')[1]
If you want to store id_number as an integer, then id_number = int(url.split('=')[1]) instead.
url.split('='), it splits the string into two parts at = and gives out an array containing them - ['https://www.exaple.com/index.php?id', '24124']. So in order to access to the value you're looking for, which is 24124, its index is 1. Therefore, url.split('=')[1].