n=int(input('Enter any number :'))
str1=""
for i in range(1,n+1):
str1+=n
print(str1)
I tried the above mentioned code and it gave me typeerror and My expectation is e.g n=5 output : 12345
n=int(input('Enter any number :'))
str1=""
for i in range(1,n+1):
str1+=n
print(str1)
I tried the above mentioned code and it gave me typeerror and My expectation is e.g n=5 output : 12345
There are two error in your code:
n
instead of i
.Now, this is my suggestion:
n=int(input('Enter any number :'))
str1=""
for i in range(1,n+1):
str1+=str(i)
print(str1)
As others mentioned, you have to cast your integer into string before concatenating, Use below, this method is called as 'list comprehension'
n=int(input('Enter any number :'))
''.join([str(i) for i in range(1,n+1)])
In Python, if you try to concatenate a string with an integer using the + operator, you will get a runtime error. That's because Python is strongly typed language. There are various other ways to perform this operation. I found a lot of similar questions asked before on Stack Overflow. For example, this one might give you your answer.