Saturday, November 23, 2024
Google search engine
HomeLanguagesPython | Padding a string upto fixed length

Python | Padding a string upto fixed length

Given a string, the task is to pad string up to given specific length with whitespaces. Let’s discuss few methods to solve the given task.
Method #1: Using ljust() 

Python3




# 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))


Output:

initial string :  123abcjw 8
final string :  123abcjw        15

  Method #2: Using format 

Python3




# 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))


Output:

initial string :  123abcjw 8
final string :  123abcjw        15

  Method #3: Using operators( * and +) 

Python3




# 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))


Output:

initial string :  123abcjw 8
final string :  123abcjw        15

Method#4: Using f-strings

Algorithm:

  1. Initialize a string ‘ini_string‘ with a value.
  2. Calculate the length of ‘ini_string’ using the ‘len()’ function.
  3. Print the initial string and its length using the ‘print()’ function and f-strings.
  4. Use the ‘f-string’ with format specifier ‘{:<N}’ to left justify the ‘ini_string’ and pad it with spaces on the right side upto ‘padding_size’ length.
  5. Store the resulting string in a new variable ‘res’.
  6. Print the final string and its length using the ‘print()’ function and f-strings.

Python3




# initialising string
ini_string = "123abcjw:, .@! eiw"
 
# create a char array from input string
# Python code to demonstrate
# padding a string upto fixed length using f-strings
 
# initialising string
ini_string = "123abcjw"
padding_size = 15
 
# printing string and its length
print(f"initial string : {ini_string} {len(ini_string)}")
 
# code to pad spaces in string
res = f"{ini_string:<{padding_size}}"
 
# printing string and its length
print(f"final string : {res} {len(res)}")


Output

initial string : 123abcjw 8
final string : 123abcjw        15

Time Complexity: The time complexity of the algorithm is O(1) as it does not depend on the length of the input string. The ‘len()’ function, f-string formatting, and string concatenation all have constant time complexity.

Space Complexity: The space complexity of the algorithm is O(1) as it only uses a fixed amount of memory to store the input string, padding size, and resulting string.

RELATED ARTICLES

Most Popular

Recent Comments