简体   繁体   中英

how to set the cursor back in previous line in python?

In one part of my Py code, I have to print an specific text per each char in a list. for example:

dictx{
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
for y in x:
    print(dictx[y])

but as you probably know, The cursor goes to the next line after each "print" so these will be written in three lines. Do you know anyway to set the cursor back? (With just a "\\t" after each print)

Use the end parameter of the print function (default value of the end parameter is '\\n' ) :

dictx = {
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
for y in x:
    print(dictx[y], end='\t')

Output:

d   f   x

I advise you to concatenate your items in a string variable, and print the final concatenated items using print. Here is the code:

dictx{
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
concatenate = ''
for y in x:
    concatenate = concatenate + '\t' + dictx[y]
print(concatenate)

You can do: print("\\t".join(dictx.values()))

This also does not require you to do the for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM