
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Justify Frame Width in Python
Suppose we have a list of words, we have to frame it in a rectangular region, line by line. See the example for better understanding.
So, if the input is like ['hello','world', 'python', 'programming','nice'], then the output will be
*************** * hello * * world * * python * * programming * * nice * ***************
To solve this, we will follow these steps −
- l:= length of maximum size word in the array
- st:= put star (l+4) times
- for each i in words, do
- st := st concatenate '*' concatenate i then add space of size (l-size of i + 1) concatenate '*'
- st:= concatenate star (l+4) times with st
- return st
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, words): l=max(len(x) for x in words) st='*'*(l+4)+'\n' for i in words: st+='* '+i+' '*(l-len(i)+1)+'*'+'\n' return st+'*'*(l+4) ob = Solution() words = ['hello','world', 'python', 'programming','nice'] print(ob.solve(words))
Input
['hello','world', 'python', 'programming','nice']
Output
*************** * hello * * world * * python * * programming * * nice * ***************
- Related Articles
- Specifying the line width of the legend frame in Matplotlib
- How to change the width of a Frame dynamically in Tkinter?
- How to justify text in label in tkinter in Python Need justify in tkinter?
- How to set the min and max height or width of a Frame in Tkinter?
- Program to find maximum width ramp in Python
- Python Tkinter – Set Entry width 100%
- How to get Linux console window width in Python?
- Adjust the width of box in boxplot in Python Matplotlib
- Getting screens height and width using Tkinter Python
- How to format a floating number to fixed width in Python?
- How to change ttk.Treeview column width and weight in Python 3.3?
- Program to find the maximum width of a binary tree in Python
- Left justify output in Java
- text-justify Property in CSS
- Using a Frame class in a Tk class in Python tkinter

Advertisements