Change language

Python String | simple (), simple (), center ()

|

These functions are:

 str.  ljust  (s, width [, fillchar]) str.  rjust  (s, width [, fillchar] ) str.  center  (s, width [, fillchar]) 

These functions respectively left-justify, right-justify, and center the string at the specified width. They return a string that is at least characters wide, created by filling the string s with fillchar (which is blank by default) to the specified width on the right, left, or both sides. The line is never truncated.

center()

This functional center aligns the line to match with the specified width and fills the remaining line space with a space if the argument & # 39;  fillchr & # 39; not transmitted.

Syntax:
center (len, fillchr)

Parameters:
len: The width of string to expand it.
fillchr (optional): The character to fill in remaining space.

Return Value:
The resultant center aligned string expanding the given width.

# Python3 demo code
# center () work

 

cstr = "I love pythonengineering"

 
# Print original line

print ( "The original string is: " , cstr, " " )

 
# Print line centered

print ( "The center aligned string is:" )

print (cstr.center ( 40 ), " " )

 
# Center printing
# line with fillchr

print ( " Center aligned string with fillchr: " )

print (cstr.center ( 40 , ’#’ ))

Output:

 The original string is : I love pythonengineering The center aligned string is: I love pythonengineering Center aligned string with fillchr: ########## I love pythonengineering ########## 

ljust ()

This function aligns a string to the specified width left and fills the remaining line space with a space if the argument & # 39;  fillchr & # 39; not transmitted.

Syntax:
ljust (len, fillchr)

Parameters:
len: The width of string to expand it.
fillchr (optional): The character to fill in remaining space.

Return Value:
The resultant left aligned string expanding the given width.

# Python3 demo code
# ljust () work

 

lstr = " I love pythonengineering "

  
# Print original line

print ( "The origi nal string is: " , lstr, " " )

 
# Left-aligned
# line with the addition & quot; - & quot;

print ( "The left aligned string is:" )

print (lstr.ljust ( 40 , ’-’ ))

Output:

 The original string is: I love pythonengineering The left aligned string is: I love pythonengineering --------------- ----- 

rjust ()

This function aligns a string to the specified width and fills in the rest request string transfer with a space if the argument & # 39;  fillchr & # 39; not transmitted.

Syntax:
rjust (len, fillchr)

Parameters:
len: The width of string to expand it.
fillchr (optional): The character to fill in remaining space.

Return Value:
The resultant right aligned string expanding the given width.

# Python3 demo code
# running rjust ()

 

rstr = "I love pythonengineering"

 
# Print original line

print ( "The original s tring is: " , rstr, " " )

 
# Print the line to the right
# with & quot; - & quot; addition

print ( "The right aligned string is: " )

print ( rstr.rjust ( 40 , ’-’ ))

Output:

 The original string is: I love pythonengineering The right aligned string is: -------------------- I love pythonengineering 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically