Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Append zeros to the end of a string

I have two strings I need to make equivalent length:

str1 ='AVFGHJK'
str2 ='ADF'

I need to append some 0s to the end of str2 so it becomes length equivalent to str1:

str1 ='AVFGHJK'
str2 ='ADF0000'

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

str2 ='ADF0000'

I have tried to use zfill, but it adds zeros to the beginning of the string, not the end.

>Solution :

Use str.ljust fo padding char

print("ABC".ljust(8, '0'))  # ABC00000

And str.rjust for leading char

print("ABC".rjust(8, '0'))  # 00000ABC
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading