Skip to content

Python Journey

My Coding Blog

Menu
  • Links
Menu

Python>>String Interpolation and Formatting

Posted on August 31, 2022August 31, 2022 by coden00b

String Interpolation is a process substituting values of variables into placeholders in a string

Few methods as follow:

a. .format() = is for simple positional formatting that  uses to  substitute in place of braces {}.

Example:

>> print (‘Welcome to {}’.format(‘ Linux Tutorials’))
Welcome to Linux Tutorials

>> title = “Linux Tutorials”
>>> print(‘Welcome to {}’.format(title))
Welcome to Linux Tutorials

>> print (‘Welcome to {} {} {}’.format (‘Linux’, ‘Free’,’Tutorials’))
Welcome to Linux Free Tutorials
>>> print (‘Welcome to {1} {0} {2}’.format (‘Linux’, ‘Free’,’Tutorials’))
Welcome to Free Linux Tutorials

>> print(‘Welcome to {1} {1}’.format(‘Free’,’Linux’))
Welcome to Linux Linux

> print (‘Welcome to {f} {l} {t}’.format(l=’linux’,f=’free’,t=’tutorials’))
Welcome to free linux tutorials

>> quotient = 200/46
>>> quotient
4.3478260869565215
>>> print(‘The quotient is {q}’.format(q=quotient))
The quotient is 4.3478260869565215

In float formatting, remember this: “{value:width.precision f}”

quotient
4.3478260869565215
>>> print(‘The quotient is {q:1.2f}’.format(q=quotient))
The quotient is 4.35
>>> print(‘The quotient is {q:1.6f}’.format(q=quotient))
The quotient is 4.347826
>>> print(‘The quotient is {q:10.1f}’.format(q=quotient))
The quotient is                4.3

 

b. f-strings (formatted strings literal) (introduced in python 3.6+)

>> title =’Free Linux Tutorials’
>>> title
‘Free Linux Tutorials’
>>> print (f’Welcome to {title}’)
Welcome to Free Linux Tutorials

>>> name = ‘Darwin’
>>> look = ‘handsome’
>>> print(f’The boy\’s name is {name} and he is {look}’)
The boy’s name is Darwin and he is handsome

>> x = 88
>>> y = 10
>>> print (f’88 multiply by 10 is {x*y}’)
88 multiply by 10 is 880

>> name = ‘Euan’
>>> sname = ‘James’
>>> age = ‘6’
>>> print(f’My son\’s name is {name} {sname} and he is {age} yrs old.’)
My son’s name is Euan James and he is 6 yrs old.

 

c. Template strings – import Template class from Python’s built-in string module to use it.

Example:

> from string import Template
>>> name = ‘Euan’
>>> sname = ‘James’
>>> full = Template(‘Hello $name $sname!’)
>>> print(full.substitute(name = name, sname = sname))
Hello Euan James!

Old style Formatting:

d. % formatting – is a unique built in operation that uses % operator to do string interpolation

Example:

>> name = ‘Euan’
>>> sname = ‘James’
>>> print(‘Hello %s %s !’%(name,sname))
Hello Euan James !

 

Take Note:
Python String Formatting Rule of Thumb:

If your format strings are user-supplied, use Template Strings to avoid security issues. Otherwise, use f-Strings if you’re on Python 3.6+, and  str.format if you’re not.

Leave a Reply Cancel reply

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

Recent Posts

  • The Zen of Python
  • Python>>String Interpolation and Formatting
  • Python>>String Methods
  • Python>>String Concatenation
  • Python>>Strings: Indexing and Slicing

Recent Comments

No comments to show.

Archives

  • January 2023
  • August 2022

Categories

  • Blog
  • Data Types