Beginner Level
Intermediate Level
Advanced Level
Introduction
When it comes to output formatting in Python, string methods provide an efficient and user-friendly way to manipulate and present data. In this tutorial, we will explore various string methods that allow us to format our output in Python. We will cover the basics of string formatting, including how to align text. By the end of this tutorial, you will have a solid understanding of how to use string methods to produce formatted output in your Python programs.
Output Formatting using string manipulation methods
- This is yet another way of displaying strings in a formatted manner.
- There are some string methods that can be used to make strings good to look at.
- Some methods which can be used to format strings for output are
- str.ljust(),
- str.rjust(), and
- str.centre()
- Sample Python code to format a output using string() method
my_str = "Hello Scholars of Kolledge.com"
# center aligning the string
print("Using fillchr to Center align the string")
print(my_str.center(50, '_'))
print()
# left aligning the string
print("Using ljust to left align the string")
print(my_str.ljust(50, '_'))
print()
# right aligning the string
print("Using rjust to right align the string")
print(my_str.rjust(50, '_'))
print()
# Output
# Using fillchr to Center align the string
# __________Hello Scholars of Kolledge.com__________
#
# Using ljust to left align the string
# Hello Scholars of Kolledge.com____________________
#
# Using rjust to right align the string
# ____________________Hello Scholars of Kolledge.com
Prev. Tutorial : Using format method
Next Tutorial : Data types in Python