| by Arround The Web | No comments

Python String Formatting Tutorial

Python is a widely utilized programming language that can be applied to various tasks such as web development, data analysis, and others. In Python, we can perform operations on the strings for multiple purposes, like adding variable expressions or adding other values to a string. These operations are referred to as string formatting.

This article will present you with a detailed guide on Python string formatting using various methods.

What is String Formatting in Python?

In Python, “String Formatting” means putting values or expressions into a string. This makes the string easier to read and understand or to change how the output looks. This method is like putting strings together without using the “+” sign or any joining tools.

How to Format a String in Python?

To format a string, the following methods are used in Python:

Method 1: Format a String in Python Using the string.format() Method

The “string.format()” method is used to format a string by adding variables, values, or expressions into it. We can add replacement fields and placeholders to a string using curly braces { } and then use the str.format() function to replace them with values passed as parameters. Here’s an example code to illustrate:

value1 = "Joseph"
value2 = 23
print("My name is {}, and I am {} years old.".format(value1, value2))

In this example, the {} curly braces act as placeholders. The .format() method replaces these placeholders with the specified values.

Output

Method 2: Format a String in Python Using the % Operator

Python has a special operator called the % operator that works with strings. It can be used for simple formatting of strings by positioning them in a certain way. This method is also known as C-style string formatting and is the oldest way of formatting strings. Here is an example code:

value1 = "Joseph"
value2 = 23
print("My name is %s, and I am %d years old."%(value1, value2))

Here, the “%s” and “%d” are format specifiers for string and integer values, respectively. The values of variables are inserted in place of these specifiers.

Output

Method 3: Format a String in Python Using the f-strings Method

The “f-strings” or string literals methods can also format strings in Python. This method is used to include Python expressions within string constants. Here’s an example to help you understand:

value1 = "Joseph"
value2 = 23
print(f"My name is {value1}, and I am {value2} years old.")

In this example, the value1 and value2 values are directly inserted into the string using the {} curly braces within the f-string.

Output

Note: To get a deeper understanding of this method, you can check this Python F-String guide.

Method 4: Format a String in Python Using the String Template Class

Template strings can also be used to format the string by importing the template class from the string module in Python’s standard library. To format strings using template strings, we need to use placeholder names that start with “$” and are valid Python identifiers. Here is an example to understand this method:

from string import Template
str1 = Template("My name is $a, and I am $b years old.")
print(str1.substitute(a="Joseph", b= 23))

Here, the template string contains placeholders prefixed with a $ sign. Next, the substitute() method replaces these placeholders with provided values.

Output

Conclusion

The “string.format()”, “% Operator”, “f-strings”, and “String Template Class” methods are used to format a string in Python. The string.format() is used to insert the variable value to the new string using the curly braces as a placeholder. The other methods can also insert the values into the string easily and efficiently. This tutorial delivered a detailed guide on Python string formatting using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply