KEMBAR78
Python Programming Essentials - M9 - String Formatting | PPTX
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
String formatting
Pavan Verma
Python Programming Essentials
@YinYangPavan
© SkillBrew http://skillbrew.com
Formatting strings
2
>>> name = "Monty Python"
>>> "Python is named after %s" % name
'Python is named after Monty Python'
>>> age = 12
>>> "this cat is %d years old" % age
'this cat is 12 years old'
Python uses
placeholders to
format strings
Strings: %s
Numbers: %d
Float: %f
© SkillBrew http://skillbrew.com
Formatting strings (2)
3
>>> os = "Android"
>>> version = 4.2
>>> "my phone uses %s %d" % (os, version)
'my phone uses Android 4'
>>> "The value of Pi is %f" % 3.141592
'The value of Pi is 3.141592'
>>> "The value of Pi is %.2f" % 3.141592
'The value of Pi is 3.14'
Use of % operator has been deprecated in Python 3.x
© SkillBrew http://skillbrew.com
Formatting strings with % operator
4
Use of % operator has been deprecated in Python 3.x
© SkillBrew http://skillbrew.com
format() method
5
>>> 'this is a sample {0}'.format('string')
'this is a sample string'
str.format()
Strings have an inbuilt method format
used to format strings
© SkillBrew http://skillbrew.com
Accessing arguments by position
6
>>> os = 'Android'
>>> version = 4.2
>>> 'phone uses {0} version {1}'.format(
os, version)
'phone uses Android version 4.2'
>>> 'version no:{1} os:{0}'.format(os, version)
'version no:4.2 os:Android'
>>> '{0} v{1} version {1}'.format(os, version)
'Android v4.2 version 4.2'
© SkillBrew http://skillbrew.com
Accessing arguments by name
7
>>> 'phone uses {os} version {ver}'.format(
os='ios', ver=7.0)
'phone uses ios version 7.0‘
>>> 'phone uses {os} version {ver}'.format(
ver=4.2, os='android')
'phone uses android version 4.2'
© SkillBrew http://skillbrew.com
Resources
 String formatting examples
http://docs.Python.org/2/library/string.html#format-examples
8
9

Python Programming Essentials - M9 - String Formatting

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself String formatting Pavan Verma Python Programming Essentials @YinYangPavan
  • 2.
    © SkillBrew http://skillbrew.com Formattingstrings 2 >>> name = "Monty Python" >>> "Python is named after %s" % name 'Python is named after Monty Python' >>> age = 12 >>> "this cat is %d years old" % age 'this cat is 12 years old' Python uses placeholders to format strings Strings: %s Numbers: %d Float: %f
  • 3.
    © SkillBrew http://skillbrew.com Formattingstrings (2) 3 >>> os = "Android" >>> version = 4.2 >>> "my phone uses %s %d" % (os, version) 'my phone uses Android 4' >>> "The value of Pi is %f" % 3.141592 'The value of Pi is 3.141592' >>> "The value of Pi is %.2f" % 3.141592 'The value of Pi is 3.14' Use of % operator has been deprecated in Python 3.x
  • 4.
    © SkillBrew http://skillbrew.com Formattingstrings with % operator 4 Use of % operator has been deprecated in Python 3.x
  • 5.
    © SkillBrew http://skillbrew.com format()method 5 >>> 'this is a sample {0}'.format('string') 'this is a sample string' str.format() Strings have an inbuilt method format used to format strings
  • 6.
    © SkillBrew http://skillbrew.com Accessingarguments by position 6 >>> os = 'Android' >>> version = 4.2 >>> 'phone uses {0} version {1}'.format( os, version) 'phone uses Android version 4.2' >>> 'version no:{1} os:{0}'.format(os, version) 'version no:4.2 os:Android' >>> '{0} v{1} version {1}'.format(os, version) 'Android v4.2 version 4.2'
  • 7.
    © SkillBrew http://skillbrew.com Accessingarguments by name 7 >>> 'phone uses {os} version {ver}'.format( os='ios', ver=7.0) 'phone uses ios version 7.0‘ >>> 'phone uses {os} version {ver}'.format( ver=4.2, os='android') 'phone uses android version 4.2'
  • 8.
    © SkillBrew http://skillbrew.com Resources String formatting examples http://docs.Python.org/2/library/string.html#format-examples 8
  • 9.

Editor's Notes

  • #7 Positioning of arguments is critical (1st snippet and 2nd snippet in this slide)