Python Tutorial

String Formatting in Python With Examples

Escape Sequence

You can't use both single and double quotations with print() function. For example, if you use "What's there?", it will result in a SyntaxError. 

>>> print("He said, "What's there?"")

...

SyntaxError: invalid syntax

>>> print('He said, "What's there?"')

...

SyntaxError: invalid syntax

To tackle this error, you can use triple quotations or escape sequences.

A backslash starts an escape sequence, which is read differently. If we express a string with a single quotation, we must escape all single quotes within the string. The same is true for double quotations. Here is an example of how the aforementioned text can be represented.

Example:

# using triple quotes

print('''He said, "What's there?"''')

# escaping single quotes

print('He said, "What\'s there?"')

# escaping double quotes

print("He said, \"What's there?\"")

Output

He said, "What's there?"
He said, "What's there?"
He said, "What's there?"

Examples

>>> print("C:\\Python32\\Lib")

C:\Python32\Lib

>>> print("This is printed\nin two lines")

This is printed

in two lines

>>> print("This is \x48\x45\x58 representation")

This is HEX representation

Raw String to ignore escape sequence

When it comes to string formatting in Python, the format() method of the string object is incredibly flexible and powerful. Curly braces are used in format strings as placeholders or replacement fields, and they are removed.

print("This is \x61 \ngood example")

This is a good example. 

print(r"This is \x61 \ngood example")

This is \x61 \ngood example

The format() Method for Formatting Strings

Format() function in Python is a solid function for the formatting of strings.  Here, you need to use curly braces as placeholders or to replacement fields. 

Example

default_order = "{}, {} and {}".format('John','Bill','Sean')

print('\n--- Default Order ---')

print(default_order)

# order using positional argument

positional_order = "{1}, {0} and {2}".format('John','Bill','Sean')

print('\n--- Positional Order ---')

print(positional_order)

# order using keyword argument

keyword_order = "{s}, {b} and {j}".format(j='John',b='Bill',s='Sean')

print('\n--- Keyword Order ---')

print(keyword_order)

Output

--- Default Order ---
John, Bill and Sean
--- Positional Order ---
Bill, John and Sean
--- Keyword Order ---
Sean, Bill and John

Optional format requirements can be sent to the format() function. To separate them from the field name, you can use a colon. Here, you can align them or make the strings left-justify, right-justify, or center align. 

The formatting of integers can be done in binary, hexadecimal, as well as some other formats. On the other hand, the floats can be made round or presented in an exponent format. You can utilise a variety of formatting options. For a complete list of Python string formatting options accessible with the format() function.

Old style formatting

We can even format strings in the same way as the old sprintf() function in the C programming language did. To do this, use the percent operator.

Code:

 y = 2.34567899

 print('The value of y is %3.2f' %y)

Output:

The value of y is 2.35

Code:

print('The value of y is %3.4f' %y)

Output:

The value of y is 2.3457
Did you find this article helpful?