How to Align Text Strings Using Python?Introduction:In this tutorial we are learning about how to Align Text Strings using Python. We will use f string to align strings in Python. Python's text alignment feature helps keep the print well and clear format. Sometimes, the files to be printed may differ in length, which can lead to inconsistencies. You can use string matching to produce string output by specifying the left, right, or center position and the space (width) allocated for the string. The text will be formatted by using the f-strings. The output string alignment is defined by the symbols' <', '>', '^' followed by a large number. The string ljust(), rjust(), and center() methods can also be used to align strings. There are some methods for Align Text Strings using Python, which are given below -
Method 1: Left alignment of the Text Strings by using the f-strings:Here, we specify '< ' to follow the numeric width of the left Alignment output string syntax. Program Code: Here we give a program code of the left alignment of the text strings by using the f-strings in Python. The code is given below - Output: Now, we run the above code and find the output string, which is printed on the left side. The output is given below - The String is Aligned into the Left!!!! Method 2: Right alignment of the Text Strings by using the f-strings:Here, we specify '>' to follow the numeric width of the Right Alignment output string syntax. Program Code: Here we give a program code of the right alignment of the text strings by using the f-strings in Python. The code is given below - Output: Now, we run the above code and find the output string, which is printed on the right side. The output is given below - The String is Aligned into the Right!!!! Method 3: Center alignment of the Text Strings by using the f-strings:Here, we specify '^' to follow the numeric width of the Centre Alignment output string syntax. Program Code: Here we give a program code of the centre alignment of the text strings by using the f-strings in Python. The code is given below - Output: Now, we run the above code and find the output string, which is printed on the center side. The output is given below - The String is Aligned into Center!!!! Method 4: Print the variable in an aligned format of the Text Strings in Python:Program Code: Here, we give a program code for printing the variable in an aligned format of the Text Strings in Python. The program below outputs 3 different strings on 3 different lines using the string f and the <, ^, > symbols respectively. The code is given below - Output: Now, we run the above code and print the output string, which is aligned with the text by using the <,^, > symbols. The output is given below - The Left Alignement The Center Alignement The Right Alignement Method 5: Print the Multiple List Values in an aligned Column format of the Text Strings in Python:Program Code: Here, we give a program code for printing the Multiple List Values in an aligned Column format of the Text Strings in Python. The program below outputs 4 different lists on 4 different aligned columns by using the string f and the <, ^, > symbols respectively. The code is given below - Output: Now, we run the above code and print the output string, which is aligned with the text by using the <,^, > symbols. The output is given below - students roll score grade Riya Paul 2 85 E Priyanka Adhikary 14 98 O Rahul Roy 20 63 B Rudra Biswas 18 74 A Neha Sen 10 66 B Method 6: Aligning the Text by using ljust(), rjust(), and centre() methods in Python:The string ljust(), rjust(), and center() methods can also be used to align the strings in Python. So now we learn about these three methods for aligning the strings, which are given below - a. ljust() method for aligning the strings: The left alignment of strings uses special characters (spaces) as padding characters. Syntax: The syntax of the ljust() method in Python is given in below - Parameters: The parameters of the ljust() method in Python are given in below - length: It is returned the length of the string. character: It is an optional parameter. The characters are written into the missing space on the right side of the string. Space ("") is the default. b. rjust() method for aligning the strings:The right alignment of strings uses special characters (spaces) as padding characters. Syntax: The syntax of the rjust() method in Python is given in below - Parameters: The parameters of the rjust() method in Python are given in below - length: It is returned the length of the string. character: It is an optional parameter. The characters are written into the missing space on the left side of the string. Space ("") is the default. c. center() method for aligning the strings:The center alignment of strings uses special characters (spaces) as padding characters. Syntax: The syntax of the center() method in Python is given in below - Parameters: The parameters of the center() method in Python are given in below - length: It is returned the length of the string. character: It is an optional parameter. It fills in the missing space on both sides with special characters. Space ("") is the default. Program Code: Here we give a program code of the string ljust(), rjust(), and center() methods that can also be used to align the strings in Python. The code is given below - Output: Now, we run the above code and print the output string, which is aligned with the text by using the ljust(), rjust(), and center() methods. The output is given below - The Left Alignment is: Javatpoint The Right Alignment is: Javatpoint The Center Alignment is: Javatpoint Method 7: Aligning the Text with the help of specified characters in Python:Program Code: Here, we give a program code for Aligning the Text with the help of specified characters in Python. The following program uses the ljust(), rjust(), and center() methods to display left alignment, right alignment, and center alignment text with the specified characters. The code is given below - Output: Now, we run the above code and print the output string, which is aligned with the text by using the ljust(), rjust(), and center() methods. Here the space is filled by the specified character. The output is given below - The Left Alignment is: Javatpoint@@@@@@@@@@ The Right Alignment is: **********Javatpoint The Center Alignment is: $$$$$Javatpoint$$$$$ Conclusion:So, in this tutorial we are learning about how to Align Text Strings using Python. Here shows how to align the strings by using the ljust(), rjust(), and center() methods in Python. We also learned how to use f-string to create different strings in different positions. Here we learn some program code for Align the given Text Strings using Python. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India