String Formatting in Python

Strings in python:

In Python, a string is a sequence of characters. It is a data type that has been derived. Strings are unchangeable. This means that once they’ve been defined, they can’t be modified. Many Python functions change strings, such as replace(), join(), and split(). They do not, however, alter the original string. They make a copy of a string, alter it, then return it to the caller.

String formatting, as the name implies, refers to the various methods for formatting strings in Python. In this article, we will go over the various methods and how to use them.

String Formatting in Python

There are several ways to format the given string in python some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1: Using % operator

Strings in Python feature a one-of-a-kind built-in operation that may be accessed with the percent operator. This allows for very basic positional formatting. If you’ve ever used a printf-style function in C, you’ll understand how this works right away. Here’s an easy example:

Below is the sample implementation:

# given website
website = "BTechGeeks"
# print the website name using % operator
print('The given website is %s' % website)

Output:

The given website is BTechGeeks

In this case, I’m using the % s format specifier to inform Python where to substitute the value of name, which is represented as a string.

Other format specifiers are provided that allow you to control the output format. To generate neatly prepared tables and reports, for example, numerals can be converted to hexadecimal notation or whitespace padding can be included.

In this case, the % x format specifier can be used to transform an int value to a string and express it as a hexadecimal number.

Method #2:Using f-strings

Python 3.6 introduced formatted string literals, also known as “f-strings,” as a new string formatting method. This new string formatting method allows you to employ embedded Python expressions within string constants. Here’s a short example to get a sense of the feature

Below is the sample implementation:

# given age value
age = 20
# multiplying age with 2
doubleAge = age*2
# print using f strings
print(f"The given age is = {age} an double the age is = {doubleAge}")

Output:

The given age is = 20 an double the age is = 40

The magic of f-strings is that you can directly embed Python expressions inside them.

As you can see, the expressions are directly embedded into the string. The string is additionally prefixed with the letter “f,” which indicates to Python that it is an f-string and that whatever expression is typed inside { }and is to be evaluated and embedded into the string at that location.

The expression inside the curly brackets does not have to be a single variable; it can be any statement that returns a value. It could be arithmetic, a function call, or a conditional operation.

Method #3:Using format() method

Python’s string .format() function debuted in version 2.6. In many aspects, it is analogous to the string modulo operator, but .format() is far more versatile. The following is the general syntax of a Python .format() is shown below:

<template>.format(<positional_argument(s)>, <keyword_argument(s)>)

It is important to note that this is a technique, not an operator. The method is invoked on template>, which is a string containing replacement fields. The method’s positional arguments> and keyword arguments> indicate values to be inserted into template> in place of the replacement fields. The formatted string that results is the method’s return value.

Replacement fields are contained in curly braces {} in the <template> string. Anything outside of curly brackets is literal text copied directly from the template to the output. If you need to include a literal curly bracket character in the template string, such as or, you can escape it by doubling it:

Below is the implementation:

# given age value
age = 20
# multiplying age with 2
doubleAge = age*2
# print using f strings
print("The given age is {one} and double the age is {sec}".format(
    one=age, sec=doubleAge))

Output:

The given age is 20 and double the age is 40

We inserted names within the placeholders in the preceding example, and we defined the value for each placeholder using its name in the format method’s argument list. The second placeholder is even supplied with zero paddings on the left, just like in the previous ways.
Related Programs: