
- 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
Python - Custom space size padding in Strings List
When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.
Example
Below is a demonstration of the same
my_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)
Output
The list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']
Explanation
A list is defined and is displayed on the console.
The lead and trail sizes are defined.
An empty list is defined.
The original list is iterated over, and the lead size and trail sizes are multiplied with empty spaces.
This is appended to the result.
This is the output that is displayed on the console.
- Related Articles
- Custom list split in Python
- Python – Filter rows without Space Strings
- Python - Convert List to custom overlapping nested list
- Custom Multiplication in list of lists in Python
- Custom sorting in list of tuples in Python
- Python - Group contiguous strings in List
- Python – Custom Lower bound a List
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Find size of a list in Python
- How to remove empty strings from a list of strings in Python?
- Concatenating two strings in MySQL with space?
- Extract numbers from list of strings in Python
- Python – Sort by Rear Character in Strings List
- Converting all strings in list to integers in Python

Advertisements