I want to print a list object with 5 items in a for loop, would like to print only the 3rd item(i.e. _ == 2
) with new line while discarding newlines for other items.
I understand that I can achieve the desired result by converting the integer '2' as string and then concatenating it with a new line character, but is there a way to specify "new" parameters based on conditions in the print function?
Here is the desired result
012
34
for _ in range(5):
print(str(_) + "\n" if _== 2 else _ , end='')
Obviously syntax error is preventing the use of new parameter in condition.
syntax_err.py
for _ in range(5):
print((_ , end='') if _!= 2 else _)
nor can I do this:
for _ in range(5):
print(_ if _== 2 else (_, end=''))