简体   繁体   中英

How to print spaces at end of a line with python?

How to print only spaces in a line or at the end of a line with Python?

If I type

print("                   ")

in fact, it only output a new line without these spaces I typed. And if I type

print("Hello              ")

there's also no spaces output at the end of line. To check if there're ending spaces printed, I copy these outputs to both Microsoft Word and Notepad, It shows that there's no spaces at the end of output line.

You can add some extra padding with ljust :

str.ljust(width, fillchar)

So simply use it in this way:

print("He" + "llo")
>> Hello

print("He".ljust(10) + "llo")
>> He        llo

end=" " gives a space at the end of string

print("Hello",end=" ")
print("World")

Output for above code :

Hello World                                                                                                                                                                          

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