Justin Chiang
Justin Chiang

Reputation: 185

How to print spaces in Python?

In C++, \n is used, but what do I use in Python?

I don't want to have to use: print (" "). This doesn't seem very elegant.

Any help will be greatly appreciated!

Upvotes: 12

Views: 333565

Answers (15)

Eduard
Eduard

Reputation: 31

A lot of users gave you answers, but you haven't marked any as an answer.

You add an empty line with print(). You can force a new line inside your string with '\n' like in print('This is one line\nAnd this is another'), therefore you can print 10 empty lines with print('\n'*10)

You can add 50 spaces inside a sting by replicating a one-space string 50 times, you can do that with multiplication 'Before' + ' '*50 + 'after 50 spaces!'

You can pad strings to the left or right, with spaces or a specific character, for that you can use .ljust() or .rjust() for example, you can have 'Hi' and 'Carmen' on new lines, padded with spaces to the left and justified to the right with 'Hi'.rjust(10) + '\n' + 'Carmen'.rjust(10)

I believe these should answer your question.

Upvotes: 0

RL-Perf
RL-Perf

Reputation: 353

Here's a short answer

x=' '

This will print one white space

print(x)

This will print 10 white spaces

print(10*x) 

Print 10 whites spaces between Hello and World

print(f"Hello{x*10}World")

Upvotes: 22

WaXxX333
WaXxX333

Reputation: 486

In Python2 there's this.

def Space(j):
    i = 0
    while i<=j:
        print " ",
        i+=1

And to use it, the syntax would be:

Space(4);print("Hello world")

I haven't converted it to Python3 yet.

Upvotes: 0

Iacchus
Iacchus

Reputation: 2837

Space char is hexadecimal 0x20, decimal 32 and octal \040.

>>> SPACE = 0x20
>>> a = chr(SPACE)
>>> type(a)
<class 'str'>
>>> print(f"'{a}'")
' '

Upvotes: 2

Arvind Reddy
Arvind Reddy

Reputation: 436

print("hello" + ' '*50 + "world")

Upvotes: 4

kamran kausar
kamran kausar

Reputation: 4603

rjust() and ljust()

test_string = "HelloWorld"

test_string.rjust(20)
'          HelloWorld'

test_string.ljust(20)
'HelloWorld          '

Upvotes: 2

SwagDaddy193
SwagDaddy193

Reputation: 1

To print any amount of lines between printed text use:

print("Hello" + '\n' *insert number of whitespace lines+ "World!")

'\n' can be used to make whitespace, multiplied, it will make multiple whitespace lines.

Upvotes: 0

PyTis
PyTis

Reputation: 1134

First and foremost, for newlines, the simplest thing to do is have separate print statements, like this:

print("Hello")
print("World.")
#the parentheses allow it to work in Python 2, or 3.

To have a line break, and still only one print statement, simply use the "\n" within, as follows:

print("Hello\nWorld.")

Below, I explain spaces, instead of line breaks...

I see allot of people here using the + notation, which personally, I find ugly. Example of what I find ugly:

x=' ';
print("Hello"+10*x+"world"); 

The example above is currently, as I type this the top up-voted answer. The programmer is obviously coming into Python from PHP as the ";" syntax at the end of every line, well simple isn't needed. The only reason it doesn't through an error in Python is because semicolons CAN be used in Python, really should only be used when you are trying to place two lines on one, for aesthetic reasons. You shouldn't place these at the end of every line in Python, as it only increases file-size.

Personally, I prefer to use %s notation. In Python 2.7, which I prefer, you don't need the parentheses, "(" and ")". However, you should include them anyways, so your script won't through errors, in Python 3.x, and will run in either.

Let's say you wanted your space to be 8 spaces, So what I would do would be the following in Python > 3.x

print("Hello", "World.",  sep=' '*8, end="\n")
# you don't need to specify end, if you don't want to, but I wanted you to know it was also an option
#if you wanted to have an 8 space prefix, and did not wish to use tabs for some reason, you could do the following.
print("%sHello World." % (' '*8))

The above method will work in Python 2.x as well, but you cannot add the "sep" and "end" arguments, those have to be done manually in Python < 3.

Therefore, to have an 8 space prefix, with a 4 space separator, the syntax which would work in Python 2, or 3 would be:

print("%sHello%sWorld." % (' '*8, ' '*4))

I hope this helps.

P.S. You also could do the following.

>>> prefix=' '*8
>>> sep=' '*2
>>> print("%sHello%sWorld." % (prefix, sep))
    Hello  World.

Upvotes: 2

Piewashere
Piewashere

Reputation: 9

simply assign a variable to () or " ", then when needed type

print(x, x, x, Hello World, x)

or something like that.

Hope this is a little less complicated:)

Upvotes: 0

Hou Lu
Hou Lu

Reputation: 3232

Sometimes, pprint() in pprint module works wonder, especially for dict variables.

Upvotes: 0

Th3carpenter
Th3carpenter

Reputation: 201

this is how to print whitespaces in python.

import string  
string.whitespace  
'\t\n\x0b\x0c\r '   
i.e .   
print "hello world"   
print "Hello%sworld"%' '   
print "hello", "world"   
print "Hello "+"world   

Upvotes: 0

user1870829
user1870829

Reputation:

Tryprint

Example:

print "Hello World!"
print
print "Hi!"

Hope this works!:)

Upvotes: 0

tkbx
tkbx

Reputation: 16305

Any of the following will work:

print 'Hello\nWorld'

print 'Hello'
print 'World'

Additionally, if you want to print a blank line (not make a new line), print or print() will work.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799570

A lone print will output a newline.

print

In 3.x print is a function, therefore:

print()

Upvotes: 6

arshajii
arshajii

Reputation: 129572

If you need to separate certain elements with spaces you could do something like

print "hello", "there"

Notice the comma between "hello" and "there".

If you want to print a new line (i.e. \n) you could just use print without any arguments.

Upvotes: 7

Related Questions