Python Format String To Specific Length

Chapter: Python Last Updated: 24-09-2023 02:58:16 UTC

Program:

            /* ............... START ............... */
                

-----------------------------------------------------------------------------------
#Using String Slicing: 

original_string = "Hello, World!"
desired_length = 15

# Truncate or pad the string to the desired length
formatted_string = original_string[:desired_length].ljust(desired_length)

print(formatted_string)

------------------------------------------------------------------------------------

#Using String Formatting:

original_string = "Hello, World!"
desired_length = 15

# Format the string to the desired length using string formatting
formatted_string = "{:<{}}".format(original_string, desired_length)

print(formatted_string)

-------------------------------------------------------------------------------------

#Using f-Strings (Python 3.6 and newer):

original_string = "Hello, World!"
desired_length = 15

# Format the string to the desired length using an f-string
formatted_string = f"{original_string:<{desired_length}}"

print(formatted_string)

-------------------------------------------------------------------------------------

# Using the str.ljust() Method:

original_string = "Hello, World!"
desired_length = 15

# Format the string to the desired length using ljust()
formatted_string = original_string.ljust(desired_length)

print(formatted_string)

-------------------------------------------------------------------------------------
                /* ............... END ............... */
        

Notes:

  • In second example, "<{}" is a format specifier that left-aligns the string and ensures it's a minimum of desired_length characters wide. If the original string is shorter, it will be padded with spaces on the right.
  • In fourth example Python's str.ljust(width, fillchar) method can be used to left-align a string and pad it with a specified character.
  • By default, str.ljust() pads with spaces. You can also specify a custom fill character if needed.
  • Choose the method that best fits your needs and coding style. These techniques allow you to format strings to a specific length by either truncating or padding them as necessary.

Tags

Python format string to specific length #How do you format a string to a certain length in Python? #ljust()

Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023

1 2 3 4