Pad or fill a string by a variable in Python using f-string Last Updated : 02 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, we can pad or fill a string with a variable character to make it a specific length using f-strings. We can pad strings to the left, right, or centre by using f-strings with a variable as the padding character. Let's understand with the help of an example: Python s = 'GFG' pad_char = '-' width = 12 padded_string = f'{s:{pad_char}^{width}}' print(padded_string) Output----GFG----- Explanation:f'{s:{pad_char}^{width}}' centers the string and pads both sides with the specified padding character (pad_char), ensuring the total width is width.Table of ContentPadding String to the Right Padding String to the Left Padding String to the CenterPadding String to the Right If we want to pad a string to the right, we can align it to the left and use a variable for the padding character. This means the string will stay at the start, and the padding character will be added after it until the string reaches the desired total length. Python s = 'GFG' char = '*' # Pad string to the right using a variable c = f"{s:{char}<10}" print(c) OutputGFG******* Padding String to the Left If we want to pad a string to the left, we can align it to the right. This means the string will move to the far right, and the padding character will be added before it until the string reaches the desired total length. Python s = 'GFG' char = '*' # Padding character # Pad string to the left using a variable left_padded = f"{s:{char}>10}" print(left_padded) Output*******GFG Padding String to the CenterIf we want to center the string and add padding on both sides, we can use the ^ symbol in f-strings. This ensures that the string will be placed in the middle, with equal padding on both sides (or as close to equal as possible). Python s = 'GFG' # Padding character char = '*' # Pad string to the center using a variable center_padded = f"{s:{char}^10}" print(center_padded) Output***GFG**** Comment More infoAdvertise with us Next Article Pad or fill a string by a variable in Python using f-string night_fury1 Follow Improve Article Tags : Python python-string Practice Tags : python Similar Reads Get Variable Name As String In Python In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods 3 min read Convert String into Variable Name in Python There may be situations where you want to convert a string into a variable name dynamically. In this article, we'll explore how to convert a string into a variable name in Python with four simple examples. Convert String into Variable Name in PythonWhile Python does not directly allow you to convert 3 min read Python - Replace all occurrences of a substring in a string Replacing all occurrences of a substring in a string means identifying every instance of a specific sequence of characters within a string and substituting it with another sequence of characters. Using replace()replace () method is the most straightforward and efficient way to replace all occurrence 2 min read How to Change Values in a String in Python The task of changing values in a string in Python involves modifying specific parts of the string based on certain conditions. Since strings in Python are immutable, any modification requires creating a new string with the desired changes. For example, if we have a string like "Hello, World!", we mi 2 min read Replacing Characters in a String Using Dictionary in Python In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement. For example, given the string "hello world" and a dictionary {'h': 'H', 'o': 'O'}, the output would be "Hel 2 min read Add padding to a string in Python Padding a string involves adding extra characters such as spaces or specific symbols to adjust its length or align it in a visually appealing way. Letâs dive into the most efficient methods to add padding to a string in Python.Using Python f-stringsF-strings allow us to specify padding directly with 2 min read String Alignment in Python f-string String alignment in Python helps make text look neat and organized, especially when printing data of different lengths. Without formatting, output can appear messy and hard to read. Pythonâs f-strings make it easy to align text by controlling its position within a set space. Pythonâs f-strings allow 2 min read Python String Formatting - How to format String? String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string.You will learn different methods of string formatting with examples for better understanding. Let's look at them now!How to Format Strings in Pyt 9 min read How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing 2 min read Is Python call by reference or call by value Python utilizes a system, which is known as "Call by Object Reference" or "Call by assignment". If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function. 5 min read Like