Padding Strings in Python

python_tutorials

Introduction

String padding refers to adding, usually, non-informative characters to a string to one or both ends of it. This is most often done for output formatting and alignment purposes, but it can have useful practical applications.

A frequent use case for padding strings is outputting table-like information in a table-like fashion. You can do this in a variety of ways, including using Pandas to convert your data to an actual table. This way, Python would handle the output formatting on its own.

In this article, we’ll cover how to pad strings in Python.

Say, we’ve got these three lists:

medicine1 = ['Morning', 'dispirine', '1 mg']
medicine2 = ['Noon', 'arinic', '2 mg']
medicine3 = ['Evening', 'Long_capsule_name', '32 mg']

We can form these into a string, using the join() function:

print(str.join(' ', medicine1))
print(str.join(' ', medicine2))
print(str.join(' ', medicine3))

Would give us the rather untidy

To finish reading, please visit source site