How to Pad a String to a Fixed Length with Spaces in Python Last Updated : 27 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Padding strings is useful for formatting data, aligning text, or preparing strings for output. The simplest and most common way to pad a string in Python is by using the built-in ljust() and rjust() methods.Using ljust() and rjust() MethodsThese methods add spaces to the left or right of the string until the string reaches a specific length.Example of ljust() (pads to the right or left justified string) Python a = "hello" # Pads 'hello' to the right with spaces ans = a.ljust(10) print(ans) Outputhello Example of rjust() (pads to the left or right justified string) Python a = "hello" # Pads 'hello' to the left with spaces result = a.rjust(10) print(result) Output hello These methods are fast and ideal for simple string padding tasks in Python. Other methods that we can use to pad a string to fixed length with spaces in python are:Table of ContentUsing Center() Using the format() Using f-strings Using center() methodThe center() method in Python pads a string to the specified length by adding spaces (or any other character) to both sides of the string. This method is particularly useful when you need to center-align text or data. Python a = "Hello" # Pads the string to a total length of 10 with spaces on both padded_a = a.center(10) print(f"'{padded_a}'") Output' Hello ' Using format() methodThe format() method in Python provides another way to pad strings. It’s more flexible than the previous methods and works well if we need to format numbers or strings in specific ways. Python a = "hello" # Pads to the right, making it 10 characters long result = "{:<10}".format(a) print(result) Outputhello "{:<10}" specifies that the string should be left-aligned and padded with spaces to make the total length 10 characters.We can also use {:>10} to pad the string to the left.Using f-strings When we are using Python 3.6 or newer, f-strings are a concise and easy-to-read way to pad strings. They allow us to specify the length directly in the f-string. Python a = "hello" # Pads to the right, making it 10 characters long result = f"{a:<10}" print(result) Outputhello The f"{a:<10}" f-string pads the string a to the right with spaces until it reaches 10 characters.You can change the alignment by using {:>10} for padding to the left. Comment More infoAdvertise with us Next Article How to Pad a String to a Fixed Length with Spaces in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-string Python string-programs Practice Tags : python Similar Reads How to Pad a String to a Fixed Length with Zeros in Python Padding a string to a fixed length with zeros in Python is basically adding leading zeros until it reaches the desired size.Using zfill() MethodThe simplest way to pad a string with zeros is by using Pythonâs built-in zfill() method. This method adds zeros to the left of a string until it reaches th 2 min read How to Create a String of Specified Length in Python Creating a string of a specific length in Python can be done using various methods, depending on the type of string and its contents. Letâs explore the most common techniques to achieve this.The simplest way to create a string of a specified length is by multiplying a character or sequence of charac 2 min read Python - Avoid Spaces in string length When working with strings in Python, we may sometimes need to calculate the length of a string excluding spaces. The presence of spaces can skew the length when we're only interested in the number of non-space characters. Let's explore different methods to find the length of a string while ignoring 3 min read Python | Padding a string upto fixed length Given a string, the task is to pad string up to given specific length with whitespaces. Let's discuss few methods to solve the given task.Method #1: Using ljust() Python3 # Python code to demonstrate # pad spaces in string # upto fixed length # initialising string ini_string = "123abcjw" p 3 min read How to Truncate a Long String after Given Size in Python? Truncating a long string to a specified size in Python can be achieved using several methods, such as slicing, string methods, and specialized libraries. Each approach is suitable for different scenarios based on your requirements. Letâs explore how to efficiently truncate strings.Using String Slici 2 min read Like