{"id":2910,"date":"2020-02-01T07:07:01","date_gmt":"2020-02-01T07:07:01","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2910"},"modified":"2023-04-05T08:42:45","modified_gmt":"2023-04-05T08:42:45","slug":"python-string-ljust-rjust-functions","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-ljust-rjust-functions","title":{"rendered":"Python String ljust() and rjust() functions"},"content":{"rendered":"\n<p>Python ljust() and rjust() functions are used to align a string, they can align a string left or right by inserting some specified character at the opposite site. For example, the ljust() function inserts the specified character to the right so the string aligns at the left. The character is whitespace by default. In this tutorial, we will learn about both these functions performing string operations.<\/p>\n\n\n\n<p><strong><em>Also Read:<a href=\"https:\/\/www.askpython.com\/python\/examples\/display-numbers-with-leading-zeros\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/examples\/display-numbers-with-leading-zeros\" rel=\"noreferrer noopener\"> How to Display Numbers with Leading Zeros in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python String ljust() function<\/h2>\n\n\n\n<p>The Python string ljust() function accepts characters for padding in an input string by adding it to the right of the input string. It also takes a length as an additional argument which decides the length of the string, if the length is less then it will keep adding the specified characters multiple times to get the required length.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.ljust(length,fillChar)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>length: <\/strong>The length specifies the width of expansion of the input string<\/li>\n\n\n\n<li><strong>fillChar:<\/strong> This is an optional parameter that determines the character insert at the right side of the string<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Value:<\/strong><\/p>\n\n\n\n<p>The ljust() function returns a new string where the given fillChar is substituted to the right of the original string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples of ljust() function in Python<\/h3>\n\n\n\n<p>Let&#8217;s now look at some examples of the ljust() function to demonstrate how it works.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_string = &#039;Python&#039;\nsize = 9\nfill_char = &#039;@&#039;\n\nprint(input_string.ljust(size, fill_char)) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nPython@@@\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str = &quot;Engineering Discipline&quot;\n\nprint (&quot;Input string: \\n&quot;,inp_str) \n\nprint (&quot;Left justified string: \\n&quot;) \nprint (inp_str.ljust(30, &#039;*&#039;)) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput string: \nEngineering Discipline\nLeft justified string: \nEngineering Discipline********\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">NumPy string operations using ljust() function<\/h3>\n\n\n\n<p>This function can also be used to perform string operations in NumPy.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.char.ljust(inputArray, length, fillChar) \n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>inputArray:<\/strong> the inputArray is a NumPy array<\/li>\n\n\n\n<li><strong>length: <\/strong>the length specifies the width of expansion of the input string<\/li>\n\n\n\n<li><strong>fillChar:<\/strong> this is an optional parameter that determines the character insert at the right side of the string<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ninput_arr = np.array(&#x5B;&#039;Safa&#039;, &#039;Aman&#039;]) \nprint (&quot;Input array : &quot;, input_arr) \n\nlen1 = 10\n\noutput_arr = np.char.ljust(input_arr, len1, fillchar =&#039;%&#039;) \nprint (&quot;Output array: &quot;, output_arr) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput array :  &#x5B;&#039;Safa&#039; &#039;Aman&#039;]\nOutput array:  &#x5B;&#039;Safa%%%%%%&#039; &#039;Aman%%%%%%&#039;]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Python String rjust() function <\/h2>\n\n\n\n<p>The Python string rjust() function accepts characters for padding in an input string by adding it to the left of the input string. It also takes a length as an additional argument which decides the length of the string, if the length is less then it will keep adding the specified characters multiple times to get the required length.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.rjust(length,fillChar)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>length: <\/strong>the length specifies the width of expansion of the input string<\/li>\n\n\n\n<li><strong>fillChar:<\/strong> this is an optional parameter that determines the character insert at the left side of the string<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Value:<\/strong><\/p>\n\n\n\n<p>The rjust() function returns a new string where the given fillChar is substituted to the left of the original string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples of rjust() function in Python<\/h3>\n\n\n\n<p>Let&#8217;s now look at some examples of the rjust() function to demonstrate how it works.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_string = &#039;Mary&#039;\nsize = 7\nfill_char = &#039;@&#039;\n\nprint(input_string.rjust(size, fill_char)) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n@@@Mary\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp_str = &quot;Engineering Discipline&quot;\n\nprint (&quot;Input string: \\n&quot;,inp_str) \n\nprint (&quot;Right justified string: \\n&quot;) \nprint (inp_str.rjust(30, &#039;*&#039;)) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput string: \n Engineering Discipline\nRight justified string: \n\n********Engineering Discipline\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">NumPy string operations using rjust() function<\/h3>\n\n\n\n<p>This function can also be used to perform string operations in NumPy.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.char.rjust(inputArray, length, fillChar) \n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>inputArray:<\/strong> the inputArray is a NumPy array<\/li>\n\n\n\n<li><strong>length: <\/strong>the length specifies the width of expansion of the input string<\/li>\n\n\n\n<li><strong>fillChar:<\/strong> this is an optional parameter that determines the character insert at the left side of the string<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n\ninput_arr = np.array(&#x5B;&#039;Safa&#039;, &#039;Aman&#039;]) \nprint (&quot;Input array : &quot;, input_arr) \n\n\nlen1 = 10\n\n\noutput_arr = np.char.rjust(input_arr, len1, fillchar =&#039;%&#039;) \nprint (&quot;Output array: &quot;, output_arr) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInput array :  &#x5B;&#039;Safa&#039; &#039;Aman&#039;]\nOutput array:  &#x5B;&#039;%%%%%%Safa&#039; &#039;%%%%%%Aman&#039;]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned to align a string in Python using two methods ljust() and rjust() which align a string left and right respectively.<\/p>\n\n\n\n<p>In this tutorial, we have covered two methods of string operation, but there are many more built-in functions available in Python for almost every operation you can perform on a string, we have a separate tutorial on that,<a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" target=\"_blank\" rel=\"noreferrer noopener\"> click here<\/a> to read.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/routines.char.html\" target=\"_blank\" rel=\"noopener\">String operations \u2014 NumPy v1.24 Manual<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python ljust() and rjust() functions are used to align a string, they can align a string left or right by inserting some specified character at the opposite site. For example, the ljust() function inserts the specified character to the right so the string aligns at the left. The character is whitespace by default. In this [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":48409,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2910"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2910\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/48409"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}