In the previous article, we have discussed Python Program for String join()
ljust() Function in Python:
The ljust() method will left align the string, using a specified character as the fill character (space is the default).
Syntax:
string.ljust(Length, character)
Parameters
Length: This is required. It is the length of the string returned.
character: This is optional. Fills the character in the missing space( right of the string). The default value is ” ” (space).
Examples:
Example1:
Input:
Given first string = "python" Given length = 15 Given character = '-'
Output:
The above given first string is : python The given first string after applying ljust() function: python---------
Note: If you don’t give any character then it takes space by default.
Example2:
Input:
Given second string = "GOODMORNING" Given length = 17 Given character = '#'
Output:
The above given second string is : GOODMORNING The given second string after applying ljust() function: GOODMORNING######
Program for ljust() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the length as static input and store it in another variable.
- Give the character as static input and store it in another variable.
- Apply ljust() method to the given string for the given length and the character which will left align the string, using a specified character as the fill character (space is the default).
- Print the above-given string.
- Print the above-given string after applying the ljust() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "python" # Give the length as static input and store it in another variable. gvn_lengt = 15 # Give the character as static input and store it in another variable. gvn_chactr = '-' # Apply ljust() method to the given string for the given length and the # character which will left align the string, using a specified character # as the fill character (space is the default). # Store it in another variable. rslt_str1 = gvn_fststr.ljust(gvn_lengt, gvn_chactr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying ljust() function. print("The given first string after applying ljust() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "GOODMORNING" gvn_lengt2 = 17 gvn_chactr2 = '#' rslt_str2 = gvn_scndstr.ljust(gvn_lengt2, gvn_chactr2) print("The above given second string is :", gvn_scndstr) print("The given second string after applying ljust() function:", rslt_str2)
Output:
The above given first string is : python The given first string after applying ljust() function: python--------- The above given second string is : GOODMORNING The given second string after applying ljust() function: GOODMORNING######
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the length as user input using the int(input()) function and store it in another variable.
- Give the character as user input using the input() function and store it in another variable.
- Apply ljust() method to the given string for the given length and the character which will left align the string, using a specified character as the fill character (space is the default).
- Print the above-given string.
- Print the above-given string after applying the ljust() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some random string = ") # Give the length as user input using the int(input()) function and store it in another variable. gvn_lengt = int(input("Enter some random number = ")) # Give the character as user input using the input() function and store it in another variable. gvn_chactr = input("Enter some random charcter = ") # Apply ljust() method to the given string for the given length and the # character which will left align the string, using a specified character # as the fill character (space is the default). # Store it in another variable. rslt_str1 = gvn_fststr.ljust(gvn_lengt, gvn_chactr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying ljust() function. print("The given first string after applying ljust() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some random string = ") gvn_lengt2 = int(input("Enter some random number = ")) gvn_chactr2 = input("Enter some random charcter = ") rslt_str2 = gvn_scndstr.ljust(gvn_lengt2, gvn_chactr2) print("The above given second string is :", gvn_scndstr) print("The given second string after applying ljust() function:", rslt_str2)
Output:
Enter some random string = hello Enter some random number = 10 Enter some random charcter = % The above given first string is : hello The given first string after applying ljust() function: hello%%%%% Enter some random string = goodmorning Enter some random number = 25 Enter some random charcter = $ The above given second string is : goodmorning The given second string after applying ljust() function: goodmorning$$$$$$$$$$$$$$
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.