1

I am trying to print text with an index and its value. This is the code

test_list = [1, 4, 5, 6, 7] 
for index, value in enumerate(test_list):
   print("S1_vec('index')<=", value)

Getting output

S1_vec('index')<= 1
S1_vec('index')<= 4
S1_vec('index')<= 5
S1_vec('index')<= 6
S1_vec('index')<= 7

But I want my output to print an index value

S1_vec('0')<= 1
S1_vec('1')<= 4
S1_vec('2')<= 5
S1_vec('3')<= 6
S1_vec('4')<= 7

Can anyone please help me to correct this issue? Thanks

2 Answers 2

4

Currently, you are passing index as literal string which is incorrect.

Use f-strings for python3.6+:

test_list = [1, 4, 5, 6, 7] 
for index, value in enumerate(test_list):
   print(f"S1_vec('{index}')<= {value}")

or format() for lower versions:

test_list = [1, 4, 5, 6, 7] 
for index, value in enumerate(test_list):
   print("S1_vec('{}')<= {}".format(index, value))
Sign up to request clarification or add additional context in comments.

Comments

0

If you're using a sufficiently recent Python, f-strings are probably easiest:

print(f"S1_vec('{index}') <= {value}")

Comments

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.