Python print number with space

Using ljust or rjust:

These python methods take a string and pad it either on the left or right to make it a set number of chars for printing to the console as you want.

To give an example with .rjust:

num = 1234 chars = 9 print(str(num).rjust(chars))

this will output:

" 1234"

or using .ljust would give you:

"1234 "

One thing to note is that .ljust and .rjust are significantly different to printing a set number of spaces ' ' before or after the int as they allow you to specify the total length of the string rather than the whitespace before it. This is a key thing to consider for example when printing a table as with these methods, the columns can be made to be set widths making it neat. Whereas simply adding spaces beforehand will result in varying widths as the number of digits of the integers will change causing the total string length to change making the widths vary.

Of course, if you wanted to, you could still specify the total string length and then calculate how many spaces need placing before or after the int.

So .rjust may be something like:

num = 1234 chars = 9 print(' ' * (chars - len(str(num))) + str(num))

noticing that here, I have concatenated the num as a string onto the whitespace with + rather than using a comma in the print statement as the comma will place an added space between the two.

and .ljust would be the same but the num printed first:

print(str(num) + ' ' * (chars - len(str(num))))

however, all this work is unnecessary and looks very messy considering you can just use the original two built-in python methods :)

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

    In this tutorial, we will be discussing how to add space in python. Adding space between variable, and string increase the readability while displaying the output of the program. We can add space in python between two variables, strings, and lines.

    • How to add space at start of string in python using rjust()
    • How to add space at end of string in python using ljust()
    • How to add space at both ends of string in python using center()
    • How to add space between two variables in python while printing
    • How to add space between lines in python while printing
    • How to add space in python using for loop
    • Conclusion

    How to add space at start of string in python using rjust()

    The rjust() method in python returns a new string. The length of the new string is provided as the input parameter. The length is increased by adding character at the left side of the original string.

    Syntax

    string.rjust(length, character)

    length – It is the length of the modified string. If the length provided is less or equal to the original string, the original string is returned.

    character –  The character parameter is optional. The given character is used to do padding at the left side of the string. The default value of character is space.

    Hence when we want to add ‘n’ number of space at the beginning of string we provide the  length equal to n + len(original_string)

    Python Examples:-

    # Python program to add space at end of the string using ljust() method # Defining the string demo_string = "My Programming Tutorial" # required length of string after adding space # where 5 is number of space to be added required_length = len(demo_string) + 5 # Using rjust() method # Not providing padding character because default character is space modified_string = demo_string.ljust(required_length) # Printing modified_string print(modified_string)

    Output:

         My Programming Tutorial

    Here in output 5 spaces are added at the start of the given input string.

    Read also: 4 Ways to count occurrences in the list in Python

    How to add space at end of string in python using ljust()

    To add space in python, at the end of the string we use ljust() method. The ljust() method in python returns a new string. a new string. The length of the new string is provided as input. The length is increased by adding character at the right side of the original string.

    Syntax

    string.ljust(length, character)

    length – It is the length of the modified string. If the length provided is less or equal to the original string, the original string is returned.

    character –  The character parameter is optional. The given character is used to do padding at the right side of the string. The default value of character is space.

    Hence when we want to add ‘n’ number of space at the beginning of string we provide the  length equal to n + len(original_string)

    Python Examples:-

    # Python program to add space at beginning of the string using rjust() method # Defining the string demo_string = "My Programming Tutorial" # required length of string after adding space # where 5 is number of space to be added required_length = len(demo_string) + 10 # Using rjust() method # Not providing padding character becuase default character is space modified_string = demo_string.center(required_length) # Printing modified_string print(modified_string)

    Output:

    My Programming Tutorial     

    Here in output 5 spaces are added at end of the given input string.

    Read also: 4 Ways to split string into list of characters

    How to add space at both ends of string in python using center()

    To add space in python, at both ends of the string we use center() method. The center() method in python returns a new string. The length of the new string is provided as input. The length is increased by adding characters on both sides of the original string.

    Syntax

    string.center(length, character)

    length – It is the length of the modified string. If the length provided is less or equal to the original string, the original string is returned.

    character –  The character parameter is optional. The given character is used to do padding at both sides of the string. The default value of character is space.

    Hence when we want to add ‘n’ number of space at the beginning of string we provide the  length equal to n + len(original_string)

    Python Examples:-

    # Python program to add space at both ends of the string using center() method # Defining the string demo_string = "My Programming Tutorial" # required length of string after adding space # where 5 is number of space to be added required_length = len(demo_string) + 10 # Using rjust() method # Not providing padding character becuase default character is space modified_string = demo_string.center(required_length) # Printing modified_string print(modified_string)

    Output:

         My Programming Tutorial     

    Here in output 5 spaces are added at both the ends of the given input string.

    Read also: Top 14 Applications of Python

    How to add space between two variables in python while printing

    In Python, we can add space between variables while printing in two ways – 

    1. Listing the variables in print() separated by a comma ” , “. For Example – print(a, b)
    2. Using format() function in print.

    To know about format function you can visit this. Both methods are shown below with examples.

    Python examples:

    # Python program to add space at variables in print() # Defining the variables demo_string = "My age is" age = 23 # Listing variable variable separated by comma in print() print("Using print and listing variables separated by comma") print(demo_string, age) print() # Using format function with print() print("Using format function with print") print("{0} {1}".format(demo_string, age))

    Output:

    Using print and listing variables separated by comma My age is 23 Using format function with print My age is 23

    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”. 

    Python Examples

    # Using n to add space between two lines in python print("Hello World.nWelcome to My Programming Tutorial..!")

    Output:

    Hello World Welcome to My Programming Tutorial..!

    How to add space in python using for loop

    In this, we will be discussing how to add space in python using for loop. We consider the problem statement as

    Input = ["I", "am", "learning", "to", "code", "in", "Python"]
    Output - I am learning to code in Python

    To print the list elements separate by space we can use for loop and print the element with the end character as space.

    By default print() in Python ends with a new line character. The print() function has a parameter called as end which can be used to specify a character that prints at the end instead of a newline character.

    Python Code:

    # Defining input string in a list input_strings = ["I", "am", "learning", "to", "code", "in", "Python"] # Using for loop to iterate over input_strings # Pritning each string with end character as space for sting in input_strings: print(sting, end = " ")

    Output:

     I am learning to code in Python 

    Conclusion

    We add space in string in python by using rjust(), ljust(), center() method. To add space between variables in python we can use print() and list the variables separate them by using a comma or by using the format() function.

    I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.

    How do you print a number and space in Python?

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

    How do you print numbers with tab spaces in Python?

    How do you print a tab character in Python? The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.

    What is %s %d %F in Python?

    Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.

    Chủ đề