For this code:
books = ["Romance","Drama","Horror"]
for number, book in enumerate(books):
print (number+1 , ".", book)
I want to get the output:
1.Romance 2.Drama 3.Horror
but I'm getting this instead:
1 . Romance 2 . Drama 3 . Horror
I tried using sys.stdout.write() instead of print() but I'm getting an error that say "expected a string or other character buffer object"
please tell me how do I disable this auto spacing thing, thanks in advance π€π
Add 'sep' keyword argument to the print function, such as print(number, ".", book, sep="")
Or
Instead of passing multiple arguments to print, pass one argument - print(str(number)+"."+book)
If you're using python3.6+, you can use f strings
kandhan
programmer
Mark
The error you got with
stdout.writeis because you need to pass a single string, not 3 things to be concatenated into a string. I.e.stdout.write("{}.{}".format(number + 1, book))The other answer using print is fine too, both ways work.