October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Pad Strings with Spaces in Python

Use ljust(), rjust(), and center() to pad Python strings with spaces, or align values in formatted output with f-strings.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use str.ljust() to add spaces on the right, str.rjust() to add them on the left, or str.center() to put them on both sides. For formatted output, use f-string alignment: < for left, > for right, and ^ for center. The requested width is a minimum, not a truncation limit.

Choose the padding direction

These string methods return a new string; they do not change the original string or print anything. The number passed to each method is the minimum total width of the result.

Goal Method Example with "cat" Result
Add spaces on the right ljust(width) "cat".ljust(6) 'cat '
Add spaces on the left rjust(width) "cat".rjust(6) ' cat'
Center with spaces center(width) "cat".center(7) ' cat '

These methods are useful when the operation is specifically about a string. The method names can feel counterintuitive at first: ljust() left-justifies the text, leaving padding on the right; rjust() right-justifies it, leaving padding on the left. See the Python 3.14.7 tutorial on input and output.

Use f-string alignment in formatted output

When inserting a value into a larger output line, specify its alignment and minimum width inside the f-string format field:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • f"{text:<6}" left-aligns the text and pads on the right.
  • f"{text:>6}" right-aligns it and pads on the left.
  • f"{text:^7}" centers it.

For example, f"{label:<12} {value:>6}" places a left-aligned label beside a right-aligned value. str.format() uses the same alignment markers: "{:<12} {:>6}".format(label, value). When you provide an alignment marker but no fill character, the format mini-language uses spaces. The Python 3.14.8 format specification documents the alignment options and minimum field width.

Use a different fill character when needed

Spaces are the default, but you can provide another fill character. For example, text.center(10, "*") centers the text using asterisks, and f"{text:*^10}" does the same with f-string formatting. In the format specification, the fill character goes immediately before the alignment marker.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Understand width and truncation

Width sets a minimum total length. If the string is already as long as or longer than the requested width, padding does not shorten it: "elephant".ljust(4) returns 'elephant'. The formatting width works the same way. If you need to truncate, do so explicitly—for example, text.ljust(width)[:width]—and consider whether removing the end of the string is acceptable.

These examples describe ordinary string-field formatting. They do not guarantee that every string will occupy the expected number of visual columns in every terminal; tabs and some Unicode characters can affect displayed alignment.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.