![]() |
VOOZH | about |
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:
----GFG-----
Explanation:
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.
GFG*******
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.
*******GFG
If 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).
***GFG****