F-strings & string formatting

The string is designed using formatting methods made available by the specific programming language. There is no end to the possibilities of string creation, from the% formatting to the format() method, to formatting string literals. In Python, there are four key ways to format strings. We will learn about the four primary methods for string formatting in Python in this lesson.

String Formatting (% Operator)

With the % operator, we can use a built-in operation in Python. This will make it easier for us to format positions simply. Anyone with even a passing familiarity with C programming is likely to have used the printf statement, which is used to print the output. The % operator is used in this sentence. The % operator in Python can be used to format strings similarly. For instance:

name=”Jack”

 n=”%s My name is %s” %name

print(n)

 Output: “My name is Jack.”

When dealing with lengthy strings, this approach has a difficulty. It will throw an error if we supply the incorrect kind of input type operator. If the input is not an integer, for instance, %d will generate a TypeError.

With Tuple ()

If we wish to create many substitutions in a single string, the syntax for string formatting, which uses the % operator, significantly alters. The % operator only accepts one parameter; to list several arguments, use tuples. The previous formatting string method is inferior to utilising tuples. It is not, however, the best method for handling lengthy strings. For instance:

name=”Jack”

class=5

s=”%s is in class %d”%(name,class)

print(s)

Output: Jack is in class 5.

String Formatting (str.format)

String formatting now has a new method thanks to Python 3. The %-operator specific syntax is removed by the format() string formatting technique, which also improves the regularity of the string formatting syntax. Multiple substitutions and value formatting are supported via str.format(). As with old-style formatting, we may perform basic positional formatting with format() We insert one or more replacement fields and placeholders denoted by a pair of curly brackets into a string using the str.format() function.

Syntax: {}.format(values)

For instance,

str = “This article is written in {} “

print (str.format(“Python”))

Output: This article is written in Python

Using f-Strings (f):

Python introduced formatted string literals, also known as “f-strings,” as a new method for formatting strings. This is a novel approach to string formatting. The use of Formatted string literals is a much more user-friendly and straightforward option. In comparison to earlier Python string formatting methods, f-string has a simple syntax. They are denoted by a “f” before the string’s initial quotation mark. For the result to be evaluated, put the expression within. Here is an easy illustration.

## declaring variables

 str1=”Python”

 str2=”Programming”

print(f”Welcome to our {str1}{str2} tutorial”)

Output: Welcome to our Python Programming tutorial.

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in