You can use format with > to right justify

N = 10
for i in range(1, N+1):
    print('{:>10}'.format('#'*i))

Output

         #
        ##
       ###
      ####
     #####
    ######
   #######
  ########
 #########
##########

You can programattically figure out how far to right-justify using rjust as well.

for i in range(1, N+1):
    print(('#'*i).rjust(N))
Answer from Cory Kramer on Stack Overflow
🌐
Programiz
programiz.com › python-programming › methods › string › rjust
Python String rjust() (With Examples)
# right aligns 'Python' up to width ... length of the string, the original string is returned. ... In the above example, we have used the rjust() method to right justify the text string....
🌐
W3Schools
w3schools.com › python › ref_string_rjust.asp
Python String rjust() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training · ❮ String Methods · Return a 20 characters long, right justified version of the word "banana": txt = "banana" x = txt.rjust(20) print(x, "is my favorite fruit.") Try it Yourself » ·
🌐
Tutorialspoint
tutorialspoint.com › python › string_rjust.htm
Python String rjust() Method
000000000000000000this is string example....wow!!! When we pass the total string length as width and letters as the fillchar parameter, the method returns the justified string to its right
🌐
Medium
medium.com › @remisharoon › mastering-text-justification-with-python-a-detailed-guide-to-leetcode-problem-68-cab95de3142
Mastering Text Justification with Python: A Detailed Guide to LeetCode Problem 68 | by Remis Haroon | Medium
July 26, 2023 - It should be left-justified, and no extra spaces should be inserted between words. This means we just join the words with a single space and pad any remaining space on the right with spaces. Now that we understand the process let’s convert our steps into Python code.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Built-in Types - str.ljust() — Python 3.11.3 documentation · Its usage is the same as rjust() and center(). Although it may not be visible in the output, trailing spaces are added when the second argument is omitted. s = 'abc' print(s.ljust(8)) ...
🌐
Rip Tutorial
riptutorial.com › justify strings
Python Language Tutorial => Justify strings
Python provides functions for justifying strings, enabling text padding to make aligning various strings much easier.
Authors: Mohit RajBhaskar N. Das
Published: 2017
Pages: 280
Find elsewhere
Top answer
1 of 2
8
  1. There's no docstring. What does this code do? How do I call it? What does it return?

  2. A class represents a group of persistent objects with common behaviour. But there are no persistent objects here, so there is no need for a class. This is also apparent from the fact that the fullJustify method does not refer to self. So don't write a class, just write a function.

  3. Python strings have a join method for concatenation. So this code:

    oneline = ""
    for ind in range(start_ind, num_of_words-1):
        oneline = oneline + words[ind] + " "
    oneline = oneline + words[num_of_words-1]
    

    can be simplified to:

    oneline = ' '.join(words[start_ind:])
    
  4. Python strings have an ljust method for left-justification within a fixed-width field. So this code:

    pad_spaces = maxWidth - len(oneline)
    oneline = oneline + " "*pad_spaces
    

    can be simplified to:

    oneline = oneline.ljust(maxWidth)
    
  5. Python has a built-in function divmod that simultaneously computes the quotient and remainder. So this code:

    basic_pad_spaces = extra_spaces // (word_num - 1)
    addition_pad_spaces = extra_spaces % (word_num - 1)
    

    can be simplied to:

    basic_pad_spaces, addition_pad_spaces = divmod(extra_spaces, word_num - 1)
    
  6. Left justification has to be done in two cases: a single word on a line, and the last line. It would therefore make sense to extract this common code into a function:

    def left_justify(words, width):
        """Given an iterable of words, return a string consisting of the words
        left-justified in a line of the given width.
    
        >>> left_justify(["hello", "world"], 16)
        'hello world     '
    
        """
        return ' '.join(words).ljust(width)
    

    Even though this is a simple one-line implementation, giving it a name improves the readability of the code where it is called. Note also the example in the docstring: this can be run and checked using the doctest module.

  7. When you are writing code that takes an input sequence (here, some words) and produces an output sequence (here, the justified lines), then it's a good idea in Python to write the code so that it iterates over the input (using for), and generates the output (using yield).

    With this approach: there's no need to keep the whole input and output sequences in memory at once (you operate on one or a few items at a time); there's no need to remember indexes into the input sequence (you just process each item as you get it); and there's no need to accumulate and return the output sequence (you just yield each item as you compute it).

    In this case:

    def justify(words, width):
        """Divide words (an iterable of strings) into lines of the given
        width, and generate them. The lines are fully justified, except
        for the last line, and lines with a single word, which are
        left-justified.
    
        >>> words = "This is an example of text justification.".split()
        >>> list(justify(words, 16))
        ['This    is    an', 'example  of text', 'justification.  ']
    
        """
        line = []             # List of words in current line.
        col = 0               # Starting column of next word added to line.
        for word in words:
            if line and col + len(word) > width:
                if len(line) == 1:
                    yield left_justify(line, width)
                else:
                    # After n + 1 spaces are placed between each pair of
                    # words, there are r spaces left over; these result in
                    # wider spaces at the left.
                    n, r = divmod(width - col + 1, len(line) - 1)
                    narrow = ' ' * (n + 1)
                    if r == 0:
                        yield narrow.join(line)
                    else:
                        wide = ' ' * (n + 2)
                        yield wide.join(line[:r] + [narrow.join(line[r:])])
                line, col = [], 0
            line.append(word)
            col += len(word) + 1
        if line:
            yield left_justify(line, width)
    
2 of 2
1

Why on Earth are you storing fullJustify as a function in the class Solution? In fact, the class solution serves no purpose whatsoever. It can be removed.

Secondly, function names and variable names should be in the style snake_case, not camelCase. Classes should be in PascalCase.

The three comments above the function Solution.fullJustify, should be stored in a docstring. Here's how you could convert those comments.

def full_justify(words, max_width):
    """
    words     - string[]
    max_width - integer
    returns  - string[]
    """
    ...

This line can be shortened from len_of_line = len_of_line + len(words[runner]) to len_of_line += len(words[runner]). There are other places you could do this. This syntax also supports other operators, like *, or /.

Finally, I find it to be clearer to declare variables on separate lines. For example, the line len_of_line, word_num_line = 0, 0 would become two separate lines, len_of_line = 0, and word_num_line = 0.

🌐
GeeksforGeeks
geeksforgeeks.org › python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - It returns an array with the elements of arr right-justified in a string of length width.It fills remaining space of each array element using fillchr parameter.If fillchr is not passed t ... String alignment in Python helps make text look neat and organized, especially when printing data of different lengths.
🌐
W3Schools
w3schools.com › python › ref_string_ljust.asp
Python String ljust() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training · ❮ String Methods · Return a 20 characters long, left justified version of the word "banana": txt = "banana" x = txt.ljust(20) print(x, "is my favorite fruit.") Try it Yourself » ·
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › rjust
Python str rjust() - Right-Justify String | Vultr Docs
December 31, 2024 - This process iterates over the names, right-justifying each one to the maximum length of the names in the list, ensuring each is aligned properly on the right.
🌐
Tutorialspoint
tutorialspoint.com › python › string_ljust.htm
Python String ljust() Method
Following is the syntax of Python String ljust() method: ... This method returns a left justified string with the fillchar specified as an argument in place of blank spaces. The original string is returned if width is less than string length.
🌐
Delft Stack
delftstack.com › home › howto › python › right justify string in python
How to Right Justify String in Python | Delft Stack
February 20, 2025 - Due to this, the program runs into the TypeError exception while executing. To right-justify the output strings, we use the length of the output string with the format specifier, as shown in the following snippet.
🌐
statyang
statyang.wordpress.com › python-practice-103-text-justification
Python practice 103: Text Justification – statyang
May 4, 2015 - Return the formatted lines as: [ “This is an”, “example of text”, “justification. ” ] Note: Each word is guaranteed not to exceed L in length. Analysis: I have an ugly solution here. Basically, it is obtained through “fail and try”. Python code: class Solution: # @param {string[]} words # @param {integer} maxWidth # @return {string[]} def fullJustify(self, words, maxWidth): res=[] words = [x for x in words if x !=""] if maxWidth<=0: return [""] if len(words)==0: return [' '*maxWidth] while len(words)>0: leftspace=maxWidth extraspace=0 i=0 while i<len(words): tmp=words[i]+' ' if
🌐
Index.dev
index.dev › blog › right-justify-objects-python-graphics
How to Right-Justify Objects in Python Graphics Using Popular Libraries
September 10, 2024 - For forms, for example, labels generally line perfectly with text fields. This is typical of professional applications where consistency is really vital. In data visualizations, where labels or legends must be precisely aligned to prevent overlapping with other graphical components, right-justification also becomes useful. Each of the various Python modules for building graphical interfaces and visualizations handles alignment in a different manner.
🌐
AskPython
askpython.com › python › string › python-string-ljust-rjust-functions
Python String ljust() and rjust() functions - AskPython
April 5, 2023 - Let’s now look at some examples of the rjust() function to demonstrate how it works. ... inp_str = "Engineering Discipline" print ("Input string: \n",inp_str) print ("Right justified string: \n") print (inp_str.rjust(30, '*'))