line0 = "hello"
line1 = "stack"
line2 = "overflow"
linenum = 1
for x in range(3):
    linenum = "line%d"%(x)
    print(linenum)
I would like to print hello stack overflow but it just prints: line0 line1 line2
line0 = "hello"
line1 = "stack"
line2 = "overflow"
linenum = 1
for x in range(3):
    linenum = "line%d"%(x)
    print(linenum)
I would like to print hello stack overflow but it just prints: line0 line1 line2
 
    
    You are constructing variable names dynamically. You can get the global namespace dictionary and look them up from there.
line0 = "hello"
line1 = "stack"
line2 = "overflow"
linenum = 1
for x in range(3):
    linenum = globals()["line%d"%(x)]
    print(linenum)
Its common to use a list or dictionary to hold data you want to lookup by way of an index or name. For instance,
lines = ["hello", "stack", "overflow"]
Whether that's the best thing in this case is a design decision for you.
 
    
    This worked:
line0 = "hello"
line1 = "stack"
line2 = "overflow"
lines = [line0, line1, line2]
ans = ""
for x in range(len(lines)):
    ans += lines[x]
print(ans)
 
    
    Below is the solution. I have used the list here. Hope it helps.
lines_list = ["hello", "stack", "overflow"]
for i in range(0, len(lines_list)):
    print (lines_list[i]),
 
    
    line0 = "hello "
line1 = "stack "
line2 = "overflow "
linenum = line0 + line1 +line2
for x in range(1):
    print(linenum)
Output: hello stack overflow.
Here is one way to print hello stack overflow. Hope this helps.
 
    
    Use list is best practice.
lines=['hello','stack','overflow']
print(*lines, sep=' ')
If you don't want use list then try this.
line0 = "hello"
line1 = "stack"
line2 = "overflow"
for x in range(3):
    exec(f'print(line{x},end=" ")')
