Connect with us

Hi, what are you looking for?

Latest

Formatting Python String- Linux Hint

 

String formatting is a major characteristic of any programming language. Through the use of string formatting, you possibly can correctly prepare your string. String formatting helps customers perceive the output in a well-defined, coherent means. In Python, string formatting might be carried out in numerous methods, i.e., through the use of the ‘%’ operator and the ‘format ()’ perform.

This text explains string formatting in Python utilizing the ‘%’ operator and the ‘format ()’ perform. The Spyder3 editor is used to put in writing and run the Python scripts proven on this article.

Format String Utilizing the % Operator

String formatting utilizing the ‘%’ operator is the oldest C-style string formatting perform. You may declare a number of variables in Python and format them through the use of the ‘%’ operator. A number of argument specifiers are used with the % operator.

The next embrace fundamental argument specifiers which are used with the ‘%’ operator:

  1. %s – Used with a string or with any string illustration, like a listing.
  2. %d – Used for integers.
  3. %f – Used for floating-point numbers.
  4. %x – Used with hex illustration.

Print String Utilizing %s

Allow us to now create a Python script and use argument specifiers for string formatting.

We now have a variable identify that incorporates the worth of the consumer identify. %s is used contained in the print perform as an argument specifier as a result of the kind of identify variable is a string.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print greeting to the consumer
identify = “Mark”
print (“Howdy %s. Welcome to the LinuxHint” %identify)

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

Print Integer Utilizing %d

Print the value of the shirt through the use of the %d argument specifier. The value worth is an integer.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print worth of the shirt
worth = 100
print (“The value of the shirt is $%d.”% worth)

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

Print Floating-Level Quantity Utilizing %f

Equally, we are able to print the value of the shirt utilizing the %f argument specifier. This time, the value worth is float.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print worth of the shirt
worth = 10.99
print (“The value of the shirt is $%f.”% worth)

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

A number of Argument Specifiers for Formatting A number of String Information

To format a number of strings, a number of specifiers are used. Parentheses [()] are used to outline the a number of specifiers.

Right here, we’ll print the identify and age of an individual utilizing a a number of argument specifier.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print identify and age
#intialize identify and age variables.
identify = “Kamran”
age = 25
print (“My identify is %s and my age is %d.” %(identify,age))

Output

The output is displayed in Python console.

Formatting Python String- Linux Hint

Allow us to now print the identify and occupation of an individual through the use of the a number of argument specifier and format the a number of string information.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print identify and age
#intialize identify and age variables.
identify = “Kamran”
occupation = “Software program Engineer”
print (“My identify is %s and I’m a %s.” %(identify,occupation))

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

You may also take the identify and occupation as an enter from the consumer, as follows:

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#print identify and age
#initialize identify and age variables.
identify = enter(“What’s your identify?n”)
occupation = enter(“What’s your occupation?n”)
print (“nMy identify is %s and I’m a %s.” %(identify,occupation))

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

You may print an object that isn’t a string utilizing the %s argument specifier. Right here, we’re printing a listing utilizing %s:

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Initialize the record
person_list = [1,”Mark”,2,”John”,3,”Taylor”]
print(“An inventory: %s” % person_list)

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

String Formatting with format () Operate

Utilizing the ‘%’ operator for string formatting is the outdated methodology. Python Three launched a brand new perform for string formatting. You may simply format a string by calling the format () perform on any sort of string object. The format () perform was later integrated in Python 2.7, as effectively.

In Python, we use positional and key phrase parameters for string formatting. Positional parameters are accessed by means of the index values, as array values are accessed utilizing an index. Key phrase parameters are accessed with a specific key phrase. We’ll see the usage of positional and key phrase parameters with the format () perform.

format () Operate Demonstration

Within the instance given beneath, we use the format () perform to format the string. The [{}] brackets are used to print the worth of the string variable. These brackets are placeholders used to outline the positional and key phrase parameters. On this instance, the brackets will likely be changed with the phrase “Instance.”

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Retailer worth in a string variable str
str = “That is an {}”
#utilizing format perform to print the worth
print(str.format(“Instance”))

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

String Formatting Utilizing A number of Placeholders

Now, we’ll use a number of placeholders [{}{}] for string formatting. On this instance, we’ll use two placeholders: the primary placeholder is for “LinuxHint,” and the second placeholder is for “net based mostly.”

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Retailer worth in a string variable str
str = “{} is a {} platform for linux associated materials.”
#utilizing format perform to print the worth
print(str.format(“LinuxHint”,”net based mostly”))

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

String Formatting Utilizing Positional Parameters

Positional parameters are accessed by the index values. The index values all the time begin from zero. On this instance, we’ll use two positional parameters, i.e., identify and age. We’ll take the identify and age from a consumer because the enter after which print the output utilizing the format () perform. The index for identify is Zero and the index for age is 1.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Retailer worth in a string variables
identify= enter(“Enter your namen”)
age=enter(“nEnter your agen”)
#utilizing format perform to print the worth
print(“My identify is {0} and my age is {1}”.format(identify,age))

Output

The output is displayed within the console.

Formatting Python String- Linux Hint

You should utilize as many positional parameters as you need. If we lengthen the earlier instance, we are able to add occupation and residence metropolis of the consumer. The index values for the occupation and residence metropolis will likely be 2 and three, respectively.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Retailer worth in a string variables
identify= enter(“Enter your namen”)
age=enter(“nEnter your agen”)
occupation= enter(“Enter your professionn”)
metropolis= enter(“Enter your residence cityn”)
#utilizing format perform to print the worth
print(“My identify is {0} and my age is {1}.I’m {2} by occupation and lives in
{3}”.format(identify,age,occupation,metropolis))

Output

The output is displayed within the Python console.

Formatting Python String- Linux Hint

String Formatting Utilizing Key phrase Parameters

You may print the identify, age, occupation, and residence metropolis of the consumer utilizing the key phrase parameters. When utilizing the key phrase parameters, you write the key phrase as an alternative of the indexes. Within the given instance, identify is the key phrase for the consumer identify, age is the key phrase for consumer age, and occupation and metropolis are the key phrases for the consumer occupation and consumer metropolis, respectively. The consumer will enter the identify, age, occupation, and metropolis. Within the formatted perform, the worth of the key phrase identify is the same as the identify variable worth. Equally, the values of the age, occupation, and metropolis key phrases are equal to their respective variables.

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
#Retailer worth in a string variables
identify= enter(“Enter your namen”)
age=enter(“Enter your agen”)
occupation= enter(“Enter your professionn”)
metropolis= enter(“Enter your residence cityn”)
#utilizing format perform to print the worth
print(“My identify is identify and my age is {age}.I’m occupation by occupation and lives in
metropolis”.format(identify=identify,age=age,occupation=occupation,metropolis=metropolis))

Output

The output is displayed on the correct facet of the Python console.

Formatting Python String- Linux Hint

Conclusion

String formatting an necessary characteristic in any programming language. It helps the consumer to enhance the output of this system. This text confirmed you two varieties of string formatting in Python: you should utilize an outdated C-style % operator, or you should utilize the format () perform. The format () perform was launched in Python Three and, in a while, it grew to become a part of Python 2.7, as effectively. This text will assist Python customers to put in writing string formatting associated scripts and packages.

python 3.7 release date,python 3.5 release date,python 3.6 end of life,python 3.6 tutorial,python f-string invalid syntax,python 3.7 keywords,bash heredoc variable,bash heredoc tutorial,echo heredoc to file,bash here string,zsh heredoc,here-document delimited by end of file,python pathlib path to string,pathlib os sep,how to give file path in python,python os.path example,how to navigate path in python,python root directory,python format number with commas,python format float,python print example,python format leading zeros,format python code,string formatting hackerrank solution,python lrm,python linux,for in python documentation,python reference,python 3.7 openssl,python syntax guide,python api documentation,python org 3.6 10 download

You May Also Like

Hosting

Introduction In previous articles we have talked about images of dockers, the origin and functioning of dockers and the dockers’ hub. In this document...

Latest

Linux desktops are good in many ways, but like Windows they are not known as the most efficient battery. This does not mean that...

Latest

The United States Supreme Court has indicated that it will finally solve a problem that has been causing legal problems for almost two decades:...

Hosting

To secure your AWS assets, follow these AWS Identity and Access Management (IAM) guidelines. Locking the Root User Access Key for AWSAccount You use...