Python|将字符串填充到固定长度
给定一个字符串,任务是用空格将字符串填充到给定的特定长度。让我们讨论一些解决给定任务的方法。
方法 #1:使用ljust()
# Python code to demonstrate
# pad spaces in string
# upto fixed length
# initialising string
ini_string = "123abcjw"
padding_size = 15
# printing string and its length
print ("initial string : ", ini_string, len(ini_string))
# code to pad spaces in string
res = ini_string.ljust(padding_size)
# printing string and its length
print ("final string : ", res, len(res))
输出:
initial string : 123abcjw 8
final string : 123abcjw 15
方法#2:使用格式
# Python code to demonstrate
# pad spaces in string
# upto fixed length
# initialising string
ini_string = "123abcjw"
# printing string and its length
print ("initial string : ", ini_string, len(ini_string))
# code to pad spaces in string
res = "{:<15}".format(ini_string)
# printing string and its length
print ("final string : ", res, len(res))
输出:
initial string : 123abcjw 8
final string : 123abcjw 15
方法 #3:使用运算符(* 和 +)
# Python code to demonstrate
# pad spaces in string
# upto fixed length
# initialising string
ini_string = "123abcjw"
padding_size = 15
# printing string and its length
print ("initial string : ", ini_string, len(ini_string))
# code to pad spaces in string
res = ini_string + " "*(padding_size - len(ini_string))
# printing string and its length
print ("final string : ", res, len(res))
输出:
initial string : 123abcjw 8
final string : 123abcjw 15