3
3
35000sobak2020-07-11 21:03:39
Python
35000sobak, 2020-07-11 21:03:39

How to properly slice in a link?

ref_id = int(message.text[7:]) #получаем id
This is how I get the id from the link of the form:
https://t.me/test_bot?start=139513584
and I get an error at the output:

ValueError: invalid literal for int() with base 10: ''

Why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SirotaKazansky, 2020-07-11
@35000sobak

Your message.text[7:] value is equal to "/t.me/test_bot?start=139513584" - how will Python assemble the int type from this?
What do you need to get as an output?
If you need a number at the end, and it is always of this length, then you need to use negative values ​​in the slice
text=" https://t.me/test_bot?start=139513584 "
ref_id = int(text[-9:])
If you pull out what is after start is equal, suddenly there is a different length or letters in general, you can try split
text=" https://t.me/test_bot?start=139513584 "
text.split("?start=")
print (text.split( "?start=")[1])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question