How do you print multiple spaces in python?

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.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will learn about how to print space or multiple spaces in the Python programming language. Spacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces.

    Following are the example of the print spaces:

    Example 1: A simple way to print spaces

    Python3

    print["GeeksForGeeks"]

    print[' ']

    print[" "]

    print["Geeks  For    Geeks"]

    Output:

    GeeksForGeeks
     
     
    Geeks  For    Geeks
    
    
    

    Example 2: Printing spaces between two values while printing in a single print statement.

    Python3

    x = 1

    y = 2

    print["x:",x]

    print["y:",y]

    print[x,"+",y,"=",x+y]

    Output:

    x: 1
    y: 2
    1 + 2 = 3
    
    
    

    Example 3: Print multiple spaces between two values.

    Python3

    print["Geeks"+" "+"For"+" "+"Geeks"]

    print["Geeks","For","Geeks"]

    print["Geeks"+" "*3+"For"+" "*3+"Geeks"]

    print["Geeks"+" "*5+"For"+" "*10+"Geeks"]

    Output:

    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    

    How do you print 3 spaces in Python?

    Call print[value] with value as " "*n to print n -many spaces on a single line.

    How do you print spacing in Python?

    How to add space between lines in python while printing. To add space in python between two lines or paragraphs we can use the new line character i.e “n”. # Using n to add space between two lines in python print["Hello World.

    How do you print two spaces with numbers in Python?

    Use print[] to add a space between two variables Call print[variable1, variable2] to print variable1 and variable2 with a space between them.

    How do you space a string in Python?

    Add Spaces in Python pandas – [Add Leading, Trailing Spaces to....
    Add Space at the start of the string in Python – rjust[] function in python..
    Add space at the end of the string in Python – ljust[] function in python..
    Add white spaces at start and end of the string in python – center[] function in python..

    Chủ Đề