m4xx
m4xx

Reputation: 13

str.replace() but new_string value should be from list

I'm trying to achieve the following,


urls=["https://example1.com/test?para=123","https://example2.com/test?para=123","https://example3.com/test?para=123"]
payload=["<test>","<test123>","<test345>"]
for i in urls:
    for j in payloads:
        print(i)
        test=i.replace("=",'='+pyloads[j])
print('\n'+test)

where I should get out put like this

https://example1.com/test?para=<test>
https://example1.com/test?para=<test123>
https://example1.com/test?para=<test345>
https://example2.com/test?para=<test>
https://example2.com/test?para=<test123>
https://example2.com/test?para=<test345>
.
.
.
https://example3.com/test?para=<test345>

But it is not seems to be working I can't iterate through the list and use it in list.

Upvotes: 1

Views: 76

Answers (4)

FAYIZ Muhammed
FAYIZ Muhammed

Reputation: 11

I will paste the fixed code here because there are a lot of problems to clear in this code.

urls=["https://example1.com/test?para=123","https://example2.com/test?para=123","https://example3.com/test?para=123"]
payload=["<test>","<test123>","<test345>"]
for i in urls:
    for j in payload:
        test=i.replace("=123", '='+j)
        print('\n'+test)

First of all, the pyloads identifier that you are using is not declared. You should use payload and the j in the for j in payloads is not a for loop but a foreach loop and return each value on every iteration instead of an index so instead of using pyloads[j] you should use only j. There are typos when you reference payload. You declared it as payload and referenced it as payloads and pyload at 2 diffeent scenarios

Second of all, in the replace function you are only replacing the '=' sign. You are not replacing the 123 in url which would result in https://example1.com/test?para=123<test> instead of https://example1.com/test?para=<test>. So in the replace function, you should use '=123'

and last the print statements are misleading as you are first printing i before replacing anything which would result in lot of unnecessary printing. And when you print test you are printing it after you replaced each URL. so put the print('\n'+test) inside the foreach block

Upvotes: 1

0x0000000000000
0x0000000000000

Reputation: 166

Your code should be like

urls=["https://example1.com/test?para=123","https://example2.com/test?para=123","https://example3.com/test?para=123"]
payload=["<test>","<test123>","<test345>"]
for i in urls:
    for j in payloads:
        print(i)
        test=i.replace("=",'='+j)
        print('\n'+test)

That is because j is not an loop index it's an element of the payloads array.

for x in list is an "foreach" iteration

Upvotes: 2

Red
Red

Reputation: 27577

You can try string slicing:

urls = ["https://example1.com/test?para=123",
        "https://example2.com/test?para=123",
        "https://example3.com/test?para=123"]
payloads = ["<test>", "<test123>", "<test345>"]

for i in urls:
    i = i[:i.index('=')]
    for j in payloads:
        print(i + j)

Output:

https://example1.com/test?para<test>
https://example1.com/test?para<test123>
https://example1.com/test?para<test345>
https://example2.com/test?para<test>
https://example2.com/test?para<test123>
https://example2.com/test?para<test345>
https://example3.com/test?para<test>
https://example3.com/test?para<test123>
https://example3.com/test?para<test345>

The str.replace() method you used wouldn't replace the text that comes after the =, hence the slicing.

Upvotes: 0

Henro Sutrisno Tanjung
Henro Sutrisno Tanjung

Reputation: 510

urls=["https://example1.com/test?para=123","https://example2.com/test?para=123","https://example3.com/test?para=123"]
payload=["<test>","<test123>","<test345>"]
for i, val in enumerate(urls):
    res = f'{val}={payload[i]}'
    print (res)


Output:
https://example1.com/test?para=123=<test>
https://example2.com/test?para=123=<test123>
https://example3.com/test?para=123=<test345>

Upvotes: 0

Related Questions