Python. String formatting operations

String formatting operations. String formatting expressions. Binary operator %


Contents


Search other resources:

1. String formatting operations. The purpose

String formatting operations allow you to substitute values of different types into strings in one step in the desired (formatted) form. Thus, the string has a convenient presentation.
For example, one useful use of string formatting operations is to output floating point values in fixed precision (a specified number of decimal places).
String formatting operations can be performed in one of two ways:

  • using string formatting expressions. These expressions are based on the model of the printf() function of C language;
  • using the format() method.

 

2. String formatting expressions. Rules of creation. Binary operator %

String formatting expressions use the printf() function model of C language. To create a string formatting expression, use the binary % operator.

In the most general case, a string formatting expression is:

format_string % (list_of_objects)

here

  • format_string – formatting string;
  • list_of_objects – objects list.

The format_string may contain:

  • string characters that are not interpreted as format specifiers. Such characters are represented as they are;
  • special characters or format specifiers preceded by a % character. In other words, if a% character is encountered in a formatted string, then one or more characters following it are treated as format specifiers. For example: “%s”, “%.2f”.

The syntax for using the format specifier is as follows:

%[(name)][flags][width][.precision]code

here

  • name – a key in the dictionary. This is the case when strings are formatted from a dictionary;
  • flags – list of flags. Using flags, you can set, for example, alignment, the presence of leading zeros (0), and the like;
  • width – the total width of the field to represent the string;
  • .precision – number of digits after the decimal point;
  • code – format specifier character (see table below). It defines how to represent data from list_of_objects.

The format specifier character can take the following values:

  • s – output the string. This uses the str(x) function, where x is an object from list_of_objects. You can read more about the str() function here;
  • r – same as s, but using the repr(x) function. The repr() function is described in detail here;
  • c – single character;
  • d – decimal integer;
  • i – an integer;
  • u – the same as d;
  • o – octal integer;
  • x – hexadecimal integer;
  • X – same as x, but hexadecimal digits are returned in uppercase;
  • e – real number in exponential form;
  • E – the same as e. In this case, alphabetic characters are returned in uppercase;
  • f, F – real number in decimal notation;
  • g, G – real number e, E or f;
  • % – symbol %.

For example, to display a real number with precision 3 decimal places, you need to specify the following format specifier

%.3f

here

  • .3 – indication that 3 decimal places are displayed;
  • f – indicates that a real number is displayed in decimal notation.

 

3. Examples of formatting integer values

Below is an example that demonstrates the use of string formatting expressions that display integers.

# String formatting expressions

# Formatting integer values
# 1. Integer specified
num = 234

# 2. Get different kinds of formatted strings
# 2.1. Present the number 234 as is
s1 = 'num=%d' % (num) # 234
print(s1)

# 2.2. Represent a number with an output width of 10 characters
s2 = 'num=%10d' % (num) # num= 234
print(s2)

# 2.3. Represent the number 234 in the 16th numeral system
s3 = 'num=%x' % (num) # num=ea
print(s3)
s4 = 'num=%X' % (num) # num=EA
print(s4)

# 2.4. Represent the number 234 in the 8th number system
s5 = 'num=%o' % (num) # num=352
print(s5)

# 3. Represent 2 numbers with a specified output width
a = -1702
b = 8833
s6 = '(a,b)=>%10d - %10d' % (a, b) # (a,b)=> -1702 - 8833
print(s6)

Program result

num=234
num= 234
num=ea
num=EA
num=352
(a,b)=> -1702 - 8833

 

4. An example that demonstrates formatting floating point numbers

 

# String formatting expressions

# Formatting floating point values
# 1. Floating point number specified
num = 234.7298027

# 2. Get different kinds of formatted strings
# 2.1. Represent number num as integer
s1 = 'num=%i' % (num) # num=234
print(s1)

# 2.2. Represent a number with precision 2 decimal places
s2 = 'num=%.2f' % (num) # num=234.73
print(s2)
s2 = 'num=%0.2F' % (num) # num=234.73
print(s2)

# 2.3. Represent a number with 4 decimal places precision and 12 digits output width
s3 = 'num=%12.4f' % (num) # num= 234.7298
print(s3)

# 2.4. Represent a number in exponential notation
s4 = 'num=%e' % (num) # num=2.347298e+02
print(s4)
s5 = 'num=%E' % (num) # num=2.347298E+02
print(s5)

# 2.5. Represent a number in exponential form with 3 decimal places precision
# and 15 digits output width
s6 = 'num=%15.3e' % (num) # num= 2.347e+02
print(s6)

# 2.6. Present a number in the format e or f
s7 = 'num=%g' % (num) # num=234.73
print(s7)

num2 = 1.234567E-03
s8 = 'num2=%G' % (num2) # num2=0.00123457
print(s8)

Program result

num=234
num=234.73
num=234.73
num= 234.7298
num=2.347298e+02
num=2.347298E+02
num= 2.347e+02
num=234.73
num2=0.00123457

 

5. An example that demonstrates formatting strings and single characters

The example demonstrates the use of format specifiers r, s, c in the formation of string formatting expressions.

# String formatting expressions

# Strings formatting
# 1. Format specifier s - uses the str() function
# 1.1. Convert integer d to string
s1 = 'str1=%s' % (235) # 'str1=235'
print(s1)
s2 = 'str2=%12s' % (1885) # 'str2= 1885'
print(s2)
s3 = '%-10s.' % (234) # '234 .'
print(s3)

# 1.2. Convert real number to string
s4 = 'str4=%s' % (-82.78234) # 'str4=-82.78234'
print(s4)
s5 = 'str5=%10s' % (-12.345) # 'str5= -12.345'
print(s5)

# 2. Format specifier r - uses the repr() function
s6 = 'str6=%r' % (777) # 'str6=777'
print(s6)
s7 = 'str7=%10r' % (1.23) # 'str7= 1.23'
print(s7)
s8 = 'str8=%r' % ('Hello!') # "str8='Hello!'"
print(s8)

# 3. Converting single characters - format specifier c
symbol = '+'
s9 = 'str9=%c' % (symbol) # 'str9=+'
print(s9)

# 4. Forming a string based on multiple specifiers
s10 = '%s%c%s' % ('bestprog', '.', 'net') # 'bestprog.net'
print(s10)

s11 = '%-10s%s' % ('Hello', 'world!') # 'Hello world!'
print(s11)

Program result

str1=235
str2= 1885
234 .
str4=-82.78234
str5= -12.345
str6=777
str7= 1.23
str8='Hello!'
str9=+
bestprog.net
Hello world!

 


Related topics