Python. Class str. Functions of strings alignment




Class str. Functions of strings alignment


Contents


Search other websites:

1. Function str.center(). Get a string of a given width

The str.center() function allows you to get a string of a given width that is centered. The general form of the function is as follows

str.center(width [, fillchar])

where

  • width – the width of the string that is centered. The width value can be both more and less than the number of characters in the string. For some string s, if width<= len(s), then the string is displayed without fillchar placeholders. If the condition width>len(s) is fulfilled for the string s, then the string is supplemented with fillchar placeholders on the left and on the right in such a way if this string were centered;
  • fillchar – placeholder. The default fillchar value is ASCII space character.

Example.

# Function str.center()
# Center the string relative to the specified width

# 1. Call with one parameter width

# case when width>len(s)
s1 = "Hello world!"
s2 = s1.center(30) # s2 = '         Hello world!         '

# case when width<=len(s)
s2 = s1.center(5) # s2 = 'Hello world!'

# 2. Call with two parameters
# case when width>len(s)
s1 = 'abcdef'
s2 = s1.center(20, '+') # s2 = '+++++++abcdef+++++++'

# case when width<=len(s)
s2 = s1.center(4, '-') # s2 = 'abcdef'

 

2. Function str.ljust().Left alignment of the string

The str.ljust() function returns a string aligned left with relative to a given width. The general form of using the function is as follows:

s2 = s1.ljust(width[, fillchar])

where

  • s1 – a string that serves as the original to create a new string s2;
  • s2 – the resulting string is padded from the end (from the right) with fillchar characters when the condition len(s1)<width is met. If the fillchar character is not present when the function is called, then by default a space is used;
  • width – alignment width. If width>len(s1), then the length of the string s2 becomes equal to width;
  • fillchar – placeholder that is appended to the left of string s1 when width>len(s1). By default, fillchar is a space character.

Example.

# Function str.ljust()
# The function returns a string aligned left relative to the specified width

# 1. Function call with 1 parameter
s1 = '0123'
s2 = s1.ljust(10) # s2 = '0123     ' - padded with spaces
s2 = s1.ljust(2) # s2 = '0123'
s2 = s1.ljust(6) # s2 = '0123   '

# 2. Function call with 2 parameters
s1 = 'abcdef'
s2 = s1.ljust(10, '+') # s2 = 'abcdef++++'
s2 = s1.ljust(3, '=') # s2 = 'abcdef'
#s2 = s1.ljust(3, '==') - error: placeholder may be one

 

3. Function str.rjust(). Right alignment of the string

The str.rjust() function returns a string aligned to the right relative to a given width. The general form of using the function is as follows:

s2 = s1.rjust(width[, fillchar])

where

  • s1 – string used to create a new string s2;
  • s2 – the resulting string, which is padded to the left with fillchar characters when the condition len(s1)<width is met. If the fillchar character is not present when the function is called, the default character is the space character;
  • width – alignment width. If width> len(s1), then the length of the string s2 becomes equal to width;
  • fillchar – placeholder that is added to the string s1 in case width>len (s1). By default, fillchar is a space character.

Example.

# Function str.rjust()
# The function returns a string aligned to the right relative to the specified width.

# 1. Function call with 1 parameter
s1 = '0123'
s2 = s1.rjust(10) # s2 = '       0123'
s2 = s1.rjust(2)   # s2 = '0123'
s2 = s1.rjust(6)   # s2 = '   0123'

# 2. Function call with 2 parameters
s1 = 'abcdef'
s2 = s1.rjust(10, '+') # s2 = '++++abcdef'
s2 = s1.rjust(3, '=')   # s2 = 'abcdef'
#s2 = s1.ljust(3, '==') - error: placeholder may be one


 

4. The function str.zfill(). Fill a string with zeros of a given width

The str.zfill() function fills the string with the character ‘0’ according to the specified width. The general form for declaring a function is as follows:

s2 = s1.zfill(width)

where

  • s1 – given string;
  • s2 – resulting string;
  • width – a number that defines the width of the fill.

Example.

# Function str.zfill()

# 1. Use without the characters '-' or '+'
s1 = '2455'
s2 = s1.zfill(8) # s2 = '00002455'

s1 = 'abcd'
s2 = s1.zfill(10) # s2 = '000000abcd'

# 2. Use with the '-' symbol
s1 = '-abcd'
s2 = str.zfill(s1, 10) # s2 = '-00000abcd'

# 3. Use with the '+' symbol
s2 = str.zfill('+abcd', 10) # s2 =   '+00000abcd'

# 4. Case when width<=len(s)
s1 = '+-=abcdef'
width = 5 # output width
s2 = s1.zfill(width) # s2 = '+-=abcdef'

 


Related topics