How to pad a string to a fixed length with spaces in Python?

后端 未结 7 2328
别跟我提以往
别跟我提以往 2020-12-08 00:06

I\'m sure this is covered in plenty of places, but I don\'t know the exact name of the action I\'m trying to do so I can\'t really look it up. I\'ve been reading an officia

相关标签:
7条回答
  • 2020-12-08 00:56

    First check to see if the string's length needs to be shortened, then add spaces until it is as long as the field length.

    fieldLength = 15
    string1 = string1[0:15] # If it needs to be shortened, shorten it
    while len(string1) < fieldLength:
        rand += " "
    
    0 讨论(0)
提交回复
热议问题