0

does anyone know how to separate this string and put it in base_url in a different position like this:

movie = ("Burning 2018")
base_url = ("http://www.omdbapi.com/?t="+(Burning)+"&y="+(2018)+"&apikey=")

This is my code:

movie = ("Burning 2018")
base_url = ("http://www.omdbapi.com/?t="+Burning+"&y="+2018+"&apikey=")
r = (requests.get(base_url))
b = r.json()
print(b)
3
  • 1
    Why are you enclosing everything in parentheses? Commented Sep 4, 2022 at 23:44
  • @Nick sorry is that a mistake? I'm new in learning python Commented Sep 5, 2022 at 0:13
  • Parentheses are not required in general, mostly they are used when you need to enforce a particular evaluation order for example a = (b + c)/d. They don't have any negative effect though, other than making the code harder to read. Commented Sep 5, 2022 at 0:27

4 Answers 4

1

You can reverse split your movie variable on space, with a maximum of 1 split, to generate the title and year. Then use an f-string to generate the base_url:

movie = 'Burning 2018'
title, year = movie.rsplit(' ', 1)
base_url = f"http://www.omdbapi.com/?t={title}&y={year}&apikey="
print(base_url)

Output:

http://www.omdbapi.com/?t=Burning&y=2018&apikey=

By using reverse split, the code will still work when a movie has more than one word in the title e.g.

movie = 'Lord of the Rings 2004'
title, year = movie.rsplit(' ', 1)
base_url = f"http://www.omdbapi.com/?t={title}&y={year}&apikey="
print(base_url)

Output:

http://www.omdbapi.com/?t=Lord of the Rings&y=2004&apikey=
Sign up to request clarification or add additional context in comments.

1 Comment

This is really a perfect answer and it goes well, my learning is fun!, I really thank you.
0
import urllib.parse

title, year = movie.rsplit(maxsplit=1)
query_string = urllib.parse.urlencode({
    "t": title,
    "y": year,
    "apikey": ""
})
base_url = "http://www.omdbapi.com/?" + query_string

rsplit splits the movie starting from the right side of the string. maxsplit=1 tells it to only split once, to handle cases where the movie title contains spaces.

urlencode builds a query string using the keys and values in the provided dict. This handles cases where the movie title contains spaces, punctuation, or other special characters.

1 Comment

this is the answer i need from yesterday,,you are a hero,i really appreciate this answer!
0

Try this:

title = movie.split()
base_url = ("http://www.omdbapi.com/?t="+title[0]+"&y="+title[1]+"&apikey=")

3 Comments

I am sorry i have typo index at title[2] i mean that title[1] read this documentation about split strings in python
How does this solve the problem? Your answer would benefit from some elaboration. Also, you should take the opportunity to remove the extraneous parentheses the OP has used.
This works well if the title is only one word,,,what if the title has multiple words? thank you for answering
-1

f-strings

question_id_no = input("What question id are you searching for?")
f"https://https://stackoverflow.com/questions/{question_id_no}"

1 Comment

This does not answer the question, nor does it offer the elaboration that should be present in a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.