
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Custom Space Size Padding in Python 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.
Advertisements