{"id":24514,"date":"2021-10-21T10:04:21","date_gmt":"2021-10-21T04:34:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=24514"},"modified":"2021-11-05T17:41:38","modified_gmt":"2021-11-05T12:11:38","slug":"python-string-rjust-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-rjust-method-examples\/","title":{"rendered":"Python String rjust() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String rindex() Method Examples<\/a> The rjust() method will right-align the string, using a specified character as the fill character (space is the default).<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n Length: <\/strong>This is required. It is the length of the string returned.<\/p>\n character:\u00a0<\/strong>This is optional. Fills the character in the missing space( left of the string). The default value is \u201d \u201d (space).<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a> Tutorial & meet such challenges with ease.<\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python String rindex() Method Examples rjust() Method in Python: The rjust() method will right-align the string, using a specified character as the fill character (space is the default). Syntax: string.rjust(Length, character) Parameters Length: This is required. It is the length of the string returned. character:\u00a0This is optional. Fills …<\/p>\n
\nrjust() Method in Python:<\/strong><\/p>\nstring.rjust(Length, character)<\/pre>\n
Given first string = \"python\"\r\nGiven length = 15 \r\nGiven character = '-'<\/pre>\n
The above given first string is : python\r\nThe given first string after applying rjust() function: ---------python<\/pre>\n
Given second string = \"GOODMORNING\"\r\nGiven length = 17\r\nGiven character = '#'<\/pre>\n
The above given second string is : GOODMORNING\r\nThe given second string after applying rjust() function: ######GOODMORNING<\/pre>\n
String rjust() Method Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\ngvn_fststr = \"python\"\r\n# Give the length as static input and store it in another variable.\r\ngvn_lengt = 15\r\n# Give the character as static input and store it in another variable.\r\ngvn_chactr = '-'\r\n# Apply rjust() method to the given string for the given length and the\r\n# character which will right align the string, using a specified character\r\n# as the fill character (space is the default).\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying rjust() function.\r\nprint(\"The given first string after applying rjust() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = \"GOODMORNING\"\r\ngvn_lengt2 = 17\r\ngvn_chactr2 = '#'\r\nrslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying rjust() function:\", rslt_str2)\r\n<\/pre>\n
The above given first string is : python\r\nThe given first string after applying rjust() function: ---------python\r\nThe above given second string is : GOODMORNING\r\nThe given second string after applying rjust() function: ######GOODMORNING<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first string as user input using the input() function and store it in a variable.\r\ngvn_fststr = input(\"Enter some random string = \")\r\n# Give the length as user input using the int(input()) function and store it in another variable.\r\ngvn_lengt = int(input(\"Enter some random number = \"))\r\n# Give the character as user input using the input() function and store it in another variable.\r\ngvn_chactr = input(\"Enter some random charcter = \")\r\n# Apply rjust() method to the given string for the given length and the\r\n# character which will right align the string, using a specified character\r\n# as the fill character (space is the default).\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying rjust() function.\r\nprint(\"The given first string after applying rjust() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = input(\"Enter some random string = \")\r\ngvn_lengt2 = int(input(\"Enter some random number = \"))\r\ngvn_chactr2 = input(\"Enter some random charcter = \")\r\nrslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying rjust() function:\", rslt_str2)<\/pre>\n
Enter some random string = hello\r\nEnter some random number = 10\r\nEnter some random charcter = %\r\nThe above given first string is : hello\r\nThe given first string after applying rjust() function: %%%%%hello\r\nEnter some random string = btechgeeks\r\nEnter some random number = 20\r\nEnter some random charcter = $\r\nThe above given second string is : btechgeeks\r\nThe given second string after applying rjust() function: $$$$$$$$$$btechgeeks<\/pre>\n
\n