-1

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.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
n00b_cod3r
  • 1
  • 1
  • 2

3 Answers3

1

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'
Mark
  • 90,562
  • 7
  • 108
  • 148
  • thank u for ur great solution but could u please tell me what's ['id'][0] , how it decided the value that id takes and split it from other strings 24124#anchor – n00b_cod3r Nov 22 '20 at 22:10
  • It knows how urls work, so ['id'] gets the `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. – Mark Nov 22 '20 at 22:14
  • Bro rsplit() is working totally fine but the issue is that it removes = while I need to use it again the idea behind this i need to take the right hand side value and re-print it with other characters for example https://www.exaple.com/index.php?id=24124 I wanna take it and re-generate it again like https://www.exaple.com/index.php?id=//24124 or https://www.exaple.com/index.php?id//=24124 or https://www.exaple.com/index.php?id=24124" etc ... so I think what I need is https://www.exaple.com/index.php?id ( in variable ) = ( in variable ) 42124 ( in variable ) – n00b_cod3r Nov 23 '20 at 00:42
0

Simply you use .split() and then take the second part only

url = 'https://www.exaple.com/index.php?id=24124'
print(url.split('=')[1])
Abdulaziz
  • 3,363
  • 1
  • 7
  • 24
0

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.

syohey
  • 156
  • 1
  • 1
  • 6
  • thanks a lot . could u please tell me what's the meaning of that [1] I mean why it's 1 – n00b_cod3r Nov 22 '20 at 22:11
  • @n00b_cod3r Yes. When you `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]`. – syohey Nov 22 '20 at 22:28