Open In App

How to use String Formatters in Python

Last Updated : 04 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Python, we use string formatting to control how text is displayed. It allows us to insert values into strings and organize the output in a clear and readable way. In this article, we’ll explore different methods of formatting strings in Python to make our code more structured and user-friendly.

Using f-string

f-strings are the simplest and most efficient way to format strings. They allow expressions to be evaluated at runtime and are preceded by “f" or “F".

Example:

# to display name and age
name = "shakshi"
age = 21
print(f"Name: {name}, Age: {age}")

# to display the sum of a and b
a = 5
b = 3
print(f"sum of {a} and {b} is {a + b}")

Output
Name: shakshi, Age: 21
sum of 5 and 3 is 8

Using format()

format() is one of the methods in string class which allows the substitution of the placeholders with the values to be formatted. 

Example:

name = "shakshi"
age = 21

# Using positional arguments
print("Name: {0}, Age: {1}".format(name, age))

# Using keyword arguments
print("Name: {name}, Age: {age}".format(name="shakshi", age=21))

Output
Name: shakshi, Age: 21
Name: shakshi, Age: 21

Explanation:

  • Positional Arguments: This inserts values in order using placeholders {}.
  • Keyword Arguments: This uses named parameters for clarity and flexibility in value assignment.

Using format specifier (% )

% operator for string formatting is the oldest method in Python and is similar to C-style string formatting. It’s still widely used but is less efficient compared to f-strings and format().

Most common format specifiers

Format specifier

                             Description                                        

%s

Specifies the String

%c

Specifies a single character

%d

Specifies the integer

%f

Specifies the float. Any number of digits can be present after decimal point

%<space>.<number>f

Specifies the float. <space> denotes the number of space to append before printing the number.

 <number> denotes the number of digits to be present after the decimal point.

%x / %X

Specifies the hexadecimal representation of the value

%o

Specifies the octal representation of a value

%e / %E

Specifies the floating numbers in exponential format

%g / %G

Similar to %e/%E. Specifies the exponential format only if the
exponent is greater than -4

Example:

a = "This is a string"
print("String is %s" % (a))

# single character
b = 'a'
print("Single character is %c" % (b))

# integer
c = 45
print("number is %d" % (c))

# float without specified precision
d = 34.521094
print("float is %f" % (d))

Output
String is This is a string
Single character is a
number is 45
float is 34.521094

Using print()

print() function in Python displays messages and outputs, converting non-string objects to strings. It also allows formatting with parameters like sep, which defines a separator between printed elements.

Example:

name = "shakshi"
age = 21
print("Name: " + name + ", Age: " + str(age))

Output
Name: shakshi, Age: 21


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg