简体   繁体   中英

How to print a each item in list without weird spacing?

I have a list gathered from a text file, f , named fList . I want to print each item in list separately on a new line and I used the following.

f.seek(0)
fList = f.readlines()
items = fList[::2] #Every other value
    
print("\n Catalogue Items: \n")
print(*items)

When printing in the console, the first item always has a weird spacing. Image of output in console 这里。
The first item always does not follow the spacing of the rest when printed.

Your text appears to include carriage return characters.

Printing *items only adds the spaces between elements, not the carriage returns, or a space before the first item.

Assuming you want those carriage returns, just add a space to the print statement (to add a space before the first element too), or use join to avoid adding the spaces between elements.

print(''.join(items)) # no spaces

Or...

print('', *items) # consistent single spaces

Or...

print(' ', '  '.join(items)) # consist double spaces

Try using the var.strip() function, it gets rid of spaces on both sides

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