String substitution methods in Python

string substitution methods in python

String substitution is a very important aspect of strings and string manipulations in Python. In this article, you’ll learn 3 ways of performing string substitutions in Python.

  • Format function
  • F-strings
  • Old string substitution

Format function

This function is one of the built-in methods for string manipulations. You are required to put curly braces in positions where you want to make substitutions in a string.

The strings that will be used for substitutions are provided as argument(s) to the function as shown below.


S = '{} shirts in the {} box'.format(5, 'blue')

print(S)

#outputs

#5 shirts in the blue box

Also, you may wish to include numbers in the curly braces, especially if you don’t want the substitution to be done based on the positions of the arguments in the format function.


S = '{0} shirts in the {1} box'.format(5, 'blue')

print(S)

#outputs

#5 shirts in the blue box

Now, if you want the first argument to substitute the second curly braces and the second argument to substitute the first curly braces, then you will give the first curly braces a number higher than the second curly braces.


S = '{1} shirts in the {0} box'.format(5, 'blue')

print(S)

#outputs

#blue shirts in the 5 box

F-strings

F-strings also known as formatted string literals are an easy way to perform string substitution in Python.

The syntax is including the letter f before the start and end quotation signs of a string. Hence, any expression inside curly braces is treated as a Python expression.


box = 'blue'

S = f'There are {2+3} shirts in the {box} box'

print(S)

#outputs

#There are 5 shirts in the blue box

Old String Substitution

This involves the use of the % operator for substitution. The syntax is as shown below:


S = '%s oranges' % 5

print(S)

In the above example, the character after the % operator is substituted by the argument provided at the end of the string, and in this case 5.

However, if more than one substitutions are to be performed, the arguments for substitution should be enclosed in parenthesis as shown below.


S = '%s shirts in the %s box' % (5, 'blue')

print(S)

S = '%.2f shirts in the %s box' % (5, 'blue')

print(S)

#outputs

#5 oranges

#5 shirts in the blue box

#5.00 shirts in the blue box

In the above code, the .2f converts the argument for the substitution into a float with a precision of 2 or 2 decimal places. In the next section, we will learn more about string formatting specifiers.

Standard string formatting specifiers

The standard string formatting specifiers are used to create a formatted string following a set of rules.

The standard format specifiers in the syntax below:

[[fill]align][sign][#][0][minimumwidth][.precision][type]

Type

The type indicates the type of data that is expected or how the data should be presented.

String Type – s

Numeric types

‘b’ binary

‘c’ character

‘d’ decimal integer

‘o’ octal

‘x’ hexadecimal

‘X’ hexadecimal

‘n’ number

‘ ‘ none


S = 'Hello there %s' % 5

print(S)

S = 'Hello there %s' % '5'

print(S)

S = 'Hello there %d' % 5

print(S)

S = 'Hello there %f' % 5

print(S)

S = 'Hello there %c' % '5'

print(S)

#outputs

#Hello there 5

#Hello there 5

##

#Hello there 5

#Hello there 5.000000

##

#Hello there 5

Other Types

Floating point types

‘e’ exponent

‘E’ exponent

‘f’ fixed point

‘F’ fixed point

‘g’ general

‘G’ general

‘n’ number

‘%’ Percentage

Time

‘a’ – abbreviated weekday name

‘A’ –  full weekday Name

‘b’ – abbreviated month name

‘B’ – full month name

‘d’ – day

‘H’ – hour with trailing zeros

‘M’ – Minutes with trailing zeros

‘S’ – Seconds with trailing zeros

‘Y’ – year with century

Precision

This is a decimal number indicating a number of digits after the decimal point. It is predominantly used if the expected argument is a float.


S = 'Hello there %.2f' % 5

print(S)

S = 'Hello there %.5f' % 5

print(S)

#outputs

#Hello there 5.00

#Hello there 5.00000

Width

This is a decimal number indicating the minimum field width. If the width argument is less than the width, then you may decide to pad the characters of your choice.


S = '%d oranges' % 5

print(S)

S = '%5d oranges' % 5

print(S)

S = '%05d oranges' % 5

print(S)

S = '%10s oranges' % 'apple'

print(S)

#outputs

#5 oranges

#5 oranges

#00005 oranges

#apple oranges

Fill

The fill is used to indicate the character to be used to pad the field to minimum width as shown below:


S = '%05d oranges' % 5

print(S)

#outputs

#00005 oranges


Leave a Reply

Your email address will not be published. Required fields are marked *