R
R
rkfddf2020-07-03 14:38:31
Python
rkfddf, 2020-07-03 14:38:31

How to concatenate in a for loop in PYTHON?

I concatenate in a loop

print(add_url)      # oborydovanie/
for i in range(1 , 10):
    add_pages.append('http://site.com/' + add_url + '?=' + str(i))
for a_pag in add_pages:
    print(a_pag)

And I get the result as
http://site.com/?=1
http://site.com/?=2

And it is necessary http://site.com/oborydovanie/?=1, that is, the add_url variable loses its value. Why is this happening?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Pankov, 2020-07-03
@rkfddf

1. Format the code with the programming language.
2. Carefully work with indents, otherwise you will confuse yourself and everyone around and will not work correctly.
3. Provide the full code so you don't have to guess what's outside of your scraps. Where is add_url assigned? Maybe there has always been empty in general and does not lose anything.
4. Read, damn it, carefully your question after writing! Are you too lazy to read? Here we are not too lazy to answer, but are you too lazy to read? What a brazen disrespect for the community? What is "add_ur"? Where is it in your code, damn it ?!
5. Use format strings to pattern things like this. They are harder to make mistakes and screw up.
I summarize. You extremely inattentively pulled lines from the code, inserted it haphazardly, missed something, broke the indents, didn’t see something, and now you want us to brew coffee thicker and wonder what you have overlooked outside of your examples.
Replace with for the third python or for the second:
'http://site.com/' + add_url + '?=' + str(i)
f'http://site.com/{add_url}?={i}'

'http://site.com/{add_url}?={i}'.format(i=i, add_url=add_url)

Type add_url right where you supply and print the result of the substitution next to it. And you will see that everything is not going smoothly for you there.

L
Larisa .•º, 2020-07-03
@barolina

nothing is lost

add_url ='oborydovanie/'
add_pages = []
for i in range(1 , 10):
  add_pages.append('http://site.com/' + add_url + '?=' + str(i))
  for a_pag in add_pages:
      print(a_pag)

# Output:
# http://site.com/oborydovanie/?=1

S
soremix, 2020-07-03
@SoreMix

It doesn't mean that there is something in your code. Variable cannot be "lost"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question