-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_padding.py
43 lines (27 loc) · 1.22 KB
/
string_padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://www.techiedelight.com/add-padding-string-python/#:~:text=The%20standard%20way%20to%20add,of%20ASCII%20space%20is%20used.
# The standard way to add padding
# if no padding is specified, the default padding of ASCII space is used
def useRjust(string, len, padding):
return string.rjust(len, padding)
# Padding paramter not supported, always filles with digit 0
def useZfill(string, len):
return string.zfill(len)
# General string formatting
def strFormat(string, len, padding):
# '{:padding>len}'.format(string)
return ('{:' + padding + '>' + str(len) + '}').format(string)
# Available on Python 3.6 or higher
def f_strings(string, len, padding):
return f'{string:{padding}>{len}}'
if __name__ == "__main__":
my_string = 'Jiwook Kim'
padding = 'X'
len = 20
print("##### Using str.rjust() #####")
print(useRjust(my_string, len, padding)) # XXXXXXXXXXJiwook Kim
print("##### Using str.zfill() #####")
print(my_string.zfill(len)) # 0000000000Jiwook Kim
print("##### Using str.format() #####")
print(strFormat(my_string, len, padding)) # XXXXXXXXXXJiwook Kim
print("##### Using f-strings #####")
print(f_strings(my_string, len, padding)) # XXXXXXXXXXJiwook Kim