/    /  Python – String Formatting

String Formatting in Python

In Python, we can format the strings by using the function format(). This format() method formats the particular values and insert inside string’s placeholder. Placeholder is defined by using curly brackets {}.

Syntax:

string.format(value1, value2...)

str.format() is one of the methods for string formatting that allows multiple substitutions and formatting value. Through this method, we can concatenate elements within the string by positional formatting. We have to give the positions of the string in the placeholders and concatenate with string passed as parameters into format() function.

We can use multiple curly braces pairs while formatting the string if there is a need for another variable substitution in sentence. Placeholders can be replaced by values in order automatically by python.

Python Code:

str = "I am good at {}"
print (str.format("Python")) 

# different datatypes can be used in formatting 
print ("Hi ! My name is {}, I am from {} department and I have {} years experience."
                            .format("Joey","Testing", 6)) 
# Positional arguments in an order 
print("{0} loves to read {1}.".format("Monica", "Novels")) 
  
#Positional arguments are not ina n order 
print("{1} likes {0}.".format("Holidays", "Students"))

Output:

I am good at Python

Hi ! My name is Joey, I am from Testing department and I have 6 years experience.

Monica loves to read Novels.

Students likes Holidays.

Positional and Keyword Argument String Formatting

The values represented within the str.format() are basically tuple data types. Each individual value in the tuple is called by its index number which ranges from 0. These indexes are passed into curly brackets which serves as placeholders in original string. If the placeholders indexes are empty, Python will replace the values through str.format() in order.

Syntax:

{0} {1}.format(positional_argument, Keyword_argument)

Where positional argument and keyword argument are parameters. Integers, Strings, Characters, Floating point numeric constants and even variables can be given as positional argument. Keyword argument is necessarily a variable which stores some value is passed as parameter.

Python Code:

# Convert base-10 decimal integers to floating point numeric constants 
print ("Rahul attained {0:f}% in his {1}!!".format(89, "Bachelor degree")) 
  
# limiting the precision 
print ("My company got {0:.2f}% as {1} this year..!".format(60.5326, "profits")) 
  
# For no decimal places 
print ("My company got {0:.0f}% as {1} this year..!".format(60.5326, "profits"))

Output:

Rahul attained 89.000000% in his Bachelor degree!!

My company got 60.53% as profits this year..!

My company got 61% as profits this year..!

Data type Conversion

We can include more parameters within the curly brackets. Data type can be converted by using field name and conversion, where field name means the index number of the argument to the str.format() method and conversion specifies the conversion code of the data type.

Conversion Codes:

s- strings

c- character

d- decimal integers

f- Floating point display

b- binary

o- octal

x- hexadecimal with lower case letters after 9

X- hexadecimal with upper case letters after 9

e- exponential notation

Syntax:

string {field_name: conversion}

Python Code:

# Convert an integer to its binary
print("The {0} of 100 is {1:b}".format("binary", 100)) 
  
# Convert an integer to its octal
print("The {0} of 100 is {1:o}".format("octal", 100))

Output:

The binary of 100 is 1100100

The octal of 100 is 144