PYTHON STRING METHODS: center(), rjust() and ljust().

Code-Parser
2,634 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

PYTHON STRING METHODS: center(), rjust() and ljust().

In this Playground tutorial we are going to look at three Python built-in functions, which are part of Python's String Methods.

The three functions we're going to look at are the center(), rjust() and ljust functions. All three are useful for alignment of strings in output.

String Method 1: center()

The center() function is a useful for centering a text or string with spaces on either side.

# Syntax:
string.center(integer)

Let's see this function in action:

String Method 2: rjust()

The rjust function is useful for aligning a text or string to the right with spaces to the left.

# Syntax:
string.rjust(integer)

Let's see an example of this function:

String Method 3: ljust()

The ljust function is useful for aligning a text or string to the left with spaces to the right.

# Syntax:
string.ljust(integer)

Let's see an example of this function:

Experiment: All three Functions

Now have a go with all three functions in the interactive editior below:

Conclusion

There we go, a brief introduction to three of Python's String Methods.

As always, I hope you've found this playground tutorial helpful.

If you'd like to see more String Methods, then here is a good link:

Happy Coding, Code-Parser

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content