Print function input into int
<p>My goal is very simple, which makes it all the more irritating that I'm repeatedly failing:</p>

<p>I wish to turn an input integer into a string made up of all numbers within the input range, so  if the input is <code>3</code>, the code would be:
<code>print(*range(1, 3+1), sep="")</code> </p>

<p>which obviously works, however when using an <code>n = input()</code> , no matter where I put the <code>str()</code>, I get the same error: </p>

<pre><code>"Can't convert 'int' object to str implicitly"
</code></pre>

<p>I feel sorry to waste your collective time on such an annoyingly trivial task..</p>

<p>My code:</p>

<pre><code>n= input()
print(*range(1, n+1), sep="")
</code></pre>

<p>I've also tried list comprehensions (my ultimate goal is to have this all on one line): </p>

<pre><code>[print(*range(1,n+1),sep="") | n = input() ]
</code></pre>

<p>I know this is not proper syntax, how on earth am I supposed to word this properly? </p>

<p><a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow">This</a> didn't help, ditto <a href="http://stackoverflow.com/questions/19352888/print-function-in-python3">this</a>, ditto <a href="http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly">this</a>, I give up --> ask S.O.</p>

<p>I see no reason why you would use <code>str</code> here, you should use <code>int</code>; the value returned from <code>input</code> is of type <code>str</code> and you need to transform it.</p>

<p>A one-liner could look like this:</p>

<pre><code>print(*range(1, int(input()) + 1), sep=' ')
</code></pre>

<p>Where <code>input</code> is wrapped in <code>int</code> to transform the str returned to an <code>int</code> and supply it as an argument to <code>range</code>.</p>

<p>As an addendum, your error here is caused by <code>n + 1</code> in your <code>range</code> call where <code>n</code> is <em>still</em> an <code>str</code>; Python <em>won't</em> implicitly transform the value held by <code>n</code> to an <code>int</code> and perform the operation; it'll complain:</p>

<pre><code>n = '1'
n + 1 
TypeErrorTraceback (most recent call last)
&lt;ipython-input-117-a5b1a168a772&gt; in &lt;module&gt;()
----&gt; 1 n + 1

TypeError: Can't convert 'int' object to str implicitly
</code></pre>

<p>That's why you need to be <em>explicit</em> and wrap it in <code>int()</code>. Additionally, take note that the one liner <em>will</em> fail with input that can't be transformed to an <code>int</code>, you need to wrap it in a <code>try-except</code> statement to handle that if needed.</p>

python python-3.x