Skip to content

String interpolation in Python

Advertisements

Example

name = 'Anand'
age = 28

strng = 'My name is ' + name + ' and I am ' + str(age) + ' years old'

print strng

#My name is Anand and I am 28 years old

Example 2

name = 'Anand'
age = 28

strng = 'I am {age} years old and my name is {name}'.format(name=name, age=age)

print strng

#I am 28 years old and my name is Anand

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.