Python. Formatting styles. String processing functions according to format or encoding rule

Formatting styles. String processing functions according to format or encoding rule


Search other websites:

1. Styles formatting of strings in the Python

In Python, when working with strings, two styles of formatting strings are supported:

  • style str.format(). This style provides more string formatting and customization options as well as custom string syntax formatting;
  • style, which is based on the use of the library function printf() of the programming language C. This style handles a smaller range of types; however, for many cases it provides faster speed of execution.


2. Function str.encode(). Return the encoded version of the string

The encode() function returns the encoded version of the string as an object of bytes. The general form of calling the encode() function is as follows:

s2 = s1.encode(encoding = "utf-8", errors = "strict")


  • s2 – result string, which is an object of bytes;
  • s1 – processed string;
  • encoding – set encoding. The default is utf-8 encoding;
  • errors – parameter that sets the error handling scheme. The value of errors can be set as ‘strict’ or ‘ignore’.


# Function encode()
s1 = "abc def ghi"

# ways to call the encode() function
s2 = str.encode(s1) # s2 = 'abc def ghi'
s3 = s1.encode() # s3 = 'abc def ghi'

# set the encoding 'utf-8' and a scheme that ignores errors
s2 = s1.encode(errors='ignore')


3. The function str.expandtabs(). Return a copy of a string, replacing tabs with a space character

The expandtabs() function returns a copy of the string in which all tabs are replaced by space characters.

According to Python documentation, the general form of using a function is as follows

s_copy = s.expandtabs(tabs = 8)


  • s – the original string that is being processed;
  • s_copy – the resulting string, which is a copy of string s with the changes made;
  • tabs – parameter determining the step of changing tab stops. The default value is tabs = 8.

The algorithm of the function is as follows. To form a new s_copy string, first the current index of this string is set to zero. Viewing the string s occurs character by character. Non-tab characters ‘\t’ are appended to the s_copy string as is. When adding each character of the string, value s changes s_copy current row index. If there is a tab character in line s, then one or more spaces are added to string s_copy until the value of the current index is equal to the next tab position. In this case, the tab character ‘\t’ itself is not copied. The step for changing the tab stops is set in the tabs parameter.

If the character of row s is the newline character ‘\n’ or the carriage return character ‘\r’, then it is copied and the current column is reset to zero.

In fact, in the result string s_copy, the text that is placed between the characters ‘\t’ of string s starts from fixed positions. The step for changing these positions is specified in the tabs parameter. For example, if the value tabs = 6, then the following positions (start indices) of the text output in the s_copy string are formed: 0, 6, 12, 18 …


# Function str.expandtabs() - tab string processing

# Original string
s = '012\t3456\t789'

# set the width of 8 characters - by default
s2 = s.expandtabs() # s2 = '012 3456 789'

# set output width to 5 characters
s2 = s.expandtabs(5) # s2 = '012 3456 789'

# set output width to 4 characters
s2 = s.expandtabs(4) # s2 = '012 3456 789'

# set output width to 3 characters
s2 = s.expandtabs(3) # s2 = '012 3456 789'

# set output width to 1 character
s2 = s.expandtabs(1) # s2 = '012 3456 789'


4. Function str.format(). String formatting

The str.format() function performs a string formatting operation. The string is processed according to the format. The format of the string specifies:

  • text from characters. This text is displayed as is;
  • replacement fields separated by braces { }. These fields are considered by the function in a special way. Each field can be replaced by the numeric index of the positional argument or the name of the key argument.

The function returns a copy of the string in which each replacement field is replaced with the string value of the corresponding argument.


# Function str.format() - string formatting operation

# Positions of arguments follow in succession: {0}, {1}, {2}
a = 5
b = 7
s = '{0} + {1} = {2}'.format(a, b, a+b) # s = '5 + 7 = 12'

# positions of arguments are mixed: {0}, {2}, {1}
x = 3.8
y = -1.9
s = '{0} ** {2} = {1}'.format(x, x**y, y)
# s = '3.8 ** -1.9 = 0.07914275540467842'

d = -388
s = 'd = {0}'.format(d) # s = 'd = -388'


Related topics