Created
June 7, 2022 01:15
-
-
Save Magnus167/77c5e23b07c3de1e318180169f3d41a9 to your computer and use it in GitHub Desktop.
apply padding to strings (or lists) in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def apply_padding(string, paddingLen, padding='.'): | |
| if len(string) %2 != paddingLen % 2: | |
| string = padding + string | |
| if len(string) >= paddingLen: | |
| return string | |
| else: | |
| return apply_padding(string=padding+string+padding, paddingLen=paddingLen) | |
| inpList = ['apple', 'banana', 'orange', 'choco'] | |
| for u in inpList: | |
| print(apply_padding(str(u), paddingLen=12, padding='.')) | |
| # inspired from soln to Coding Exercise: Centering Text, https://cscircles.cemc.uwaterloo.ca/8-remix/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment