How to Format String in Python | EP-27 String Formatting in Python | Python Tutorials
String Formatting
"""String formating is also known as string interpolation.
Interpolation:String interpolation is a process substituting values of variables into
placeholders in a string. For instance, if you have a template for saying hello to a person
like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder
for name of person with an actual name.
string formatting can be done in various ways
1.% formatting
2. str.formate()function
3. f-string"""
1. % formatting
"""Oldest technique used to insert an object into a string."""
company = "cybrosys"
city= "calicut"
print("Company name is %s." %company)
"""Multiple values are allowed in string formatting"""
print("Company name is %s and located at %s."%(company,city))
2. str.formate()function
"""The placeholders are replaced by curly braces in this technique"""
company = "cybrosys"
city= "calicut"
print("Company name is {} and located at {}.".format(company,city))
"""Refering variable through indexing is possible"""
company = "cybrosys"
city= "calicut"
print("Company name is {0} and located at {1}. ".format(company,city),":indexing")
"""to improve the readability,keyword also used"""
C = "cybrosys"
c = "calicut"
print("Company name is {Company} and located at {City}. ".format(Company=C,City=c),"::::::::")
"""Formate specifies like f for float, b for binary , d for integers can be used"""
print("I have scored {:f}% mark in english".format(55.66))
"""An integer can be provided in place of float but float cannot be provided in place of
integer"""
print("I have scored {:f}% mark in maths".format(55))
print("I have scored {:d}% mark in english".format(55))
"""Providing a sting in place of float will result in an error"""
print("I have scored {:d}% mark in english".format('55'))
"""The length of value after the decimal point can be controlle"""
print("I have scored {:.1f}% mark in maths".format(55.789654626549865))
3. f-string
"""used to format a string
syntax : f"string{object} """
company = "cybrosys"
city= "calicut"
print(f"company name is {company} and city is {city} ")
print(F'company name is {company} and city is {city}')
#PythonTutorials #StringFormatting #PythonForBeginners #LearnPython #CodingTips #PythonProgramming #Programming #PythonTricks #CodingTutorial #Developer #python #pythontutorial #coding #tutorial #pythontips #python3 #python2 #fstrings #formatmethod #percentformatting #pythondeveloper #datascience #machinelearning #deeplearning
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys