I am trying to generate URLs as follows: Show http://ergast.com/api/f1/2000/qualifying?limit=10000 I am using Python to generate URLs for the years 2000 to 2015, and to that end, wrote this code snippet:
The output is:
How do I get rid of the first line? I tried making the range (2000, 2016), but the same thing happened with the first line being 2000 instead of 1999. What am I doing wrong? How can I fix this? asked Apr 13, 2016 at 9:09
CodingInCirclesCodingInCircles 2,38510 gold badges57 silver badges84 bronze badges You can use string formatting for this:
UPDATE: Based on OP's comments to pass these urls in
answered Apr 13, 2016 at 9:14
1 Instead of using the URL to join the string, use a list comprehension to create the different URLs.
You could then still use answered Apr 13, 2016 at 9:14
tobias_ktobias_k 79.4k11 gold badges114 silver badges171 bronze badges You could use the cleaner and more powerful string formatting as follows,
In your code the use of answered Apr 13, 2016 at 12:55
C PandaC Panda 3,1072 gold badges10 silver badges11 bronze badges 2 It's good to understand why it's happening. For that you need to understand the join function, look the docs
That means that your url parameter will be repeated in between the words you want to concatenate, what will result in the output above, with the first element without the url. What you want is not use join, is to concatenate the strings as you're already doing with the year. For that you can use different methods, as was already answered. You can use string formatting as was pointed out by @AKS and it should work. answered Apr 13, 2016 at 9:19
dfrancadfranca 4,9192 gold badges30 silver badges57 bronze badges How do you join a loop in python?Python join string and int
Here, we used the map function along with string join to achieve the same result. Refer to the map function. It is another approach to concatenating string and int using the join method. Here, we are using the format function inside the for loop to format list items.
How do I concatenate a string in a for loop?This rule finds code that performs string concatenation in a loop using the + operator. If the enclosing loop in question is executed many times, the use of the + operator may be inefficient.
Can we join list in python?Python Join List. We can use the python string join() function for joining a list of the strings. However, this function takes iterable as an argument and the list is iterable, so we are able to use it with the list.
How do you join a list in python?One of the easiest ways are by using the + operator.. Join two list: list1 = ["a", "b", "c"] list2 = [1, 2, 3] list3 = list1 + list2. ... . Append list2 into list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] for x in list2: ... . Use the extend() method to add list2 at the end of list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3]. |