slices to the rescue :)

def left(s, amount):
    return s[:amount]

def right(s, amount):
    return s[-amount:]

def mid(s, offset, amount):
    return s[offset:offset+amount]
Answer from Andy W on Stack Overflow
🌐
Dot Net Perls
dotnetperls.com › right-python
Python - String Right Part - Dot Net Perls
December 13, 2021 - Consider the string "soft orange cat." The last 3 characters are "cat," and so the result of right() with an argument of 3 should be "cat." ... Here we introduce a "right" method. Pay close attention to the return value of right().
Discussions

Why does Python prefer the right side to the left side?
Because the operator “or” works like this: if the first item is true (or evaluates to true), return it otherwise, return the second item More on reddit.com
🌐 r/Python
37
67
August 7, 2023
How do I align/move text right?
Spaces = 12 For i in range(1,13): Print(" " * (spaces-i) + "*" * i) More on reddit.com
🌐 r/learnpython
13
3
June 9, 2020
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Use the rjust(), center(), or ljust() methods to right-justify, center, or left-justify strings str in Python. You can convert numbers (int and float) to strings using str() and apply these methods. R ...
🌐
Trymito
trymito.io › excel-to-python › functions › text › RIGHT
Excel to Python: RIGHT Function - A Complete Guide | Mito
Excel's RIGHT function is used to extract a specific number of characters from the right end of a text string. This can be especially useful in data preprocessing tasks like extracting the last name from a full name or extracting a unit label ...
🌐
W3Schools
w3schools.com › python › ref_string_rjust.asp
Python String rjust() Method
Note: In the result, there are actually 14 whitespaces to the left of the word banana. The rjust() method will right align the string, using a specified character (space is default) as the fill character.
🌐
STEMpedia
ai.thestempedia.com › home › python functions › right()
right() - Motion Library - Python Function
July 25, 2022 - right() function is from Motion library of PictoBlox Python. The function turns the sprite by the specified amount of degrees clockwise. This changes the direction the sprite is facing.
Find elsewhere
🌐
InterviewQs
interviewqs.com › ddi-code-snippets › substring-python
Slice a string in python (right, left, mid equivalents) - InterviewQs
Interview Questions Pricing Sign in · Back to snippets · import pandas as pd · Create some dummy data · string = '8754321' string · '8754321' #right 2 characters string[-2:] '21' #left 2 characters string[:2] '87' #4th through 6th characters (index starts at 0) string[4:6] '32'
🌐
Tutorialspoint
tutorialspoint.com › python › string_rjust.htm
Python String rjust() Method
Following is the syntax for Python String rjust() method − ... This method returns the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space).
🌐
GeeksforGeeks
geeksforgeeks.org › python-right-and-left-shift-characters-in-string
Python - Right and Left Shift characters in String - GeeksforGeeks
April 16, 2025 - For example, let's take a string s = "geeks" and we need to perform a left and right shift of k=2 places: ... This article explores different ways to perform such shifts in Python using different methods, let's explore them with examples:
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › rjust
Python str rjust() - Right-Justify String | Vultr Docs
December 31, 2024 - This procedure aligns each column's headers and data entries to the right, ensuring all elements are vertically aligned and properly formatted. The str.rjust() method in Python simplifies the process of aligning text to the right, a common ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › turtle-right-method-in-python
turtle.right() method in Python - GeeksforGeeks
August 18, 2025 - Interview Questions · Examples ... Updated : 18 Aug, 2025 · turtle.right() method turns the turtle clockwise by the given angle (in degrees) without changing its position....
🌐
Thedeveloperblog
thedeveloperblog.com › python › right-python
Python Right String Part
C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML ... Get the right part of strings by using a negative starting slice index.
🌐
ListenData
listendata.com › home › python
String Functions in Python with Examples
The table below shows many common string functions in Python along with their descriptions and their equivalent functions in MS Excel. If you are intermediate MS Excel users, you must have used LEFT, RIGHT and MID Functions.
🌐
Programiz
programiz.com › python-programming › methods › string › rjust
Python String rjust() (With Examples)
The rjust() method right aligns the string up to a given width using a specified character.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - Notice that the start or end index can be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (from the right to left).
🌐
Reddit
reddit.com › r/learnpython › how do i align/move text right?
r/learnpython on Reddit: How do I align/move text right?
June 9, 2020 -

Hi all! So I’ve been trying to learn python by giving myself small tasks to do and I decided I wanted to make a right aligned right triangle (I know it’s really specific) but it never works, I’ve tried a few different things but I am never able to align it to the right, it always aligns left I have everything else correct though

        *
      **
    ***
  ****
*****

Is what it’s supposed to look like

for a in range (-1, -6, -1):
  for b in range (a, 0):
     print ("*",end="")
  print ("")

There is my code, if someone could guide me on what I’m doing wrong/ how to fix it I’d be super grateful!

Sorry for formatting, am on mobile

🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.sql › api › pyspark.sql.functions.right.html
pyspark.sql.functions.right — PySpark 4.1.2 documentation
Returns the rightmost len`(`len can be string type) characters from the string str, if len is less or equal than 0 the result is an empty string. New in version 3.5.0.
🌐
Delft Stack
delftstack.com › home › howto › python › right justify string in python
How to Right Justify String in Python | Delft Stack
February 20, 2025 - All the approaches discussed above are used to format variables with every data type. Python also provides us with methods specific to strings. We can use the rjust() method to right justify strings in python.
Top answer
1 of 1
8

Cheating

Both functions look like you've failed to do the assignment. The problem description talks about the len(...) function, and how it behaves with strings. You are not using it to write the function. Ok, maybe it doesn't explicitly say you have to use it, but it seems implied! Instead, you've scoured the standard library and determined two different functions which do the work for you. I think the point of the question is re-inventing the wheel ... writing a function like .rjust(w) yourself!

Which option is better?

Consider what the functions do:


With '{:>70}'.format(s), the format function has to scan the '{:>70}' string for {}, break it up into field and format codes, take arguments from the .format(...) parameter and/or keyword list, and interpolate those into the string at the appropriate places, applying the required formatting. In short, it powerful, but computationally expensive.

If s is not a string, the function will still work, implicitly formatting the result with str(s), which is one of the advantages of the additional power in this approach.


With s.rjust(70), it is taking a string and padding it on the left with (by default) spaces. Simple and fast.

If s is not a string (or another class which defines .rjust()), it will raise an exception.


So, ...

  • the first is "better", in terms of the Robustness Principle in the sense that it works with more arguments.
  • the second is "better" in terms of efficiency

Additional possibilities

You can cheat and write the function even shorter, using f-strings:

def right_justify(s: str) -> None:
    """
    Print out the given argument right-justified to 70 characters,
    followed by a newline.

    Note: If the string is longer than 70 characters, it is printed
    without modification.
    """

    print(f'{s:>70}')

Well, it was shorter until I added argument type-hints, a return value type-hint, and a """docstring""" ... which are all "best practices".

But, the assignment goal is still not satisfied. Without these magical format functions or .rjust methods, can you write a function which will accomplish the goal yourself?