Python: Probably More than 90% of Coders are Missing This Easy Trick

<p>If you are already familiar with&nbsp;<strong>online interviews</strong>&nbsp;and&nbsp;<strong>coding practice websites</strong>, you would for sure, love this easy Python feature.</p> <p>Read this completely for a better understanding!</p> <p>For example, I ask you to&nbsp;<strong>print a list of values</strong>. You would do that using a&nbsp;<code>for</code>&nbsp;loop in no time.</p> <pre> li = [10, 20, 30, 40, 50] for i in li: print(i)</pre> <pre> Output: 10 20 30 40 50</pre> <p>But now, I ask if you can&nbsp;<strong>print the values in the same line with a space between each element</strong>. You would still make that look easy by using the&nbsp;<strong>end argument</strong>&nbsp;within&nbsp;<code>print()</code>.</p> <pre> li = [10, 20, 30, 40, 50] for i in li: print(i, end=&#39; &#39;)</pre> <pre> Output: 10 20 30 40 50 </pre> <h2>The Problem</h2> <p>Now, here is the exact problem. The output when seen looks like what I asked for. But when you select the output, an&nbsp;<strong>annoying space pops out</strong>&nbsp;of nowhere.</p> <p>And that is because the last loop prints 50 and then adds a space after it with the use of the&nbsp;<strong>end argument</strong>. Though the end argument helped us in printing space in between, this side effect of putting a white space at the last is not what we wanted.</p> <p>Especially, when it comes to online interview questions and coding platforms, they expect us to&nbsp;<strong>get the exact output as given</strong>. Even an extra space would result in the website not accepting our solution.</p> <p><a href="https://python.plainenglish.io/python-probably-more-than-90-of-coders-are-missing-this-easy-trick-1f5e1c2d8561">Click Here</a></p>
Tags: Python Trick