SlideShare a Scribd company logo
1 of 16
Python-Basics
Strings-Text Processing
Guru
Page 1
Agenda
• Escape Sequences
• Raw Strings
• Doc Strings
• More formatting
• Conversions
• String Methods
• The string module
Page 2
Escapes and Special Characters
• Certain special characters have special values
• They do not have a corresponding print character
• Instead they have an effect
• For example, newline, carriage return
• To insert a special character, you “escape” it
• Using the backslash
Page 3
Escape Characters
• Newline ('n')
• Carriage return ('r')
• Single quote (''')
• Double quote (""")
• Backspace ('b')
• Tab ('t')
• Vertical tab ('v')
• Backslash ('')
Page 4
Raw Strings
• The raw_input( ) function reads a line from
standard input as a raw string
• Used in interactive console programs
Page 5
Quoting
• Can use either single or double
• This makes embedded quotes easy
• Can also “escape” characters with 
• Triple quotes define “long strings”
• String that can span multiple lines
• Often used in documentation and web pages
Page 6
str( ) vs. repr( )
• str( ) converts an object to a string
• It renders a human-readable string
• print uses str( ) (and expands escapes)
• repr( ) renders a string that can be evaluated by the
interpreter
• As if typed at the >>> prompt
• So it executes correctly
• Also can use backquotes
• The interpreter uses repr( ) to echo variables
• Differences show up in numbers and escape
sequences
Page 7
str( ) vs. repr( )
Examples
>>> x = .1
>>> x
0.10000000000000001
>>> str(x)
'0.1‘
>>> repr(x)
'0.10000000000000001‘
>>> `x`
'0.10000000000000001‘
>>> s = "Hellonthere"
>>> s
'Hellonthere'
>>> print s
Hello
there
>>> repr(s)
"'Hellonthere'"
Page 8
Question
• What is the result of executing:
str(2+3)
?
Page 9
Format Descriptors
• %s (string, via str( ))
• %r (string, via repr( ))
• %c (character)
• %d (decimal integer)
• %x (hex integer)
• %X (uppercase hex)
• %e (scientific notation for reals)
• %f (fixed-point decimal for reals)
• %g (attempts %f; bails to %e)
Page 10
String Methods
• A lot!
• Called as <string-var>.<method>(…)
• For example, s.count('a')
• capitalize, center, count, endswith, find,
index, isalpha, isdigit (etc., as in C), istitle,
join, lower, replace, split, strip, swapcase,
title, translate, upper, zfill
Page 11
Using split( )
• Splits a string into substrings
• Uses whitespace as a separator(default)
• You can provide your own separator
• It uses the whole separator string
• See next slide
• The re module has a better version of split
Page 12
Using split( ) and join( )
Example
>>> s = 'a,b,c d,e‘
>>> s.split()
['a,b,c', 'd,e']
>>> s.split(',')
['a', 'b', 'c d', 'e']
>>> ','.join(['x','y','z'])
'x,y,z‘
>>> ''.join(['x','y','z'])
'xyz'
Page 13
Exercise
• Print the words from a sentence in the
reverse order of the appearance,
• Write a module that reads a string from the
console and tests to see if it is a palindrome
(spelled the same backward as forward)
(Hint: for both of these, the reverse( ) list method comes in
handy)
Page 14
Questions
Page 15
Imagination Action Joy

More Related Content

Similar to Strings.ppt

Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsYashJain47002
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
Basics of Python
Basics of PythonBasics of Python
Basics of PythonEase3
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentationAliul Kadir Akib
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfpaijitk
 
Console I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxConsole I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxPRASENJITMORE2
 

Similar to Strings.ppt (20)

matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
matlab_tutorial.ppt
matlab_tutorial.pptmatlab_tutorial.ppt
matlab_tutorial.ppt
 
Python course
Python coursePython course
Python course
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Regular expression for everyone
Regular expression for everyoneRegular expression for everyone
Regular expression for everyone
 
Basics of Python
Basics of PythonBasics of Python
Basics of Python
 
Python study material
Python study materialPython study material
Python study material
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
PPS_Unit 4.ppt
PPS_Unit 4.pptPPS_Unit 4.ppt
PPS_Unit 4.ppt
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
The string class
The string classThe string class
The string class
 
Console I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxConsole I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptx
 
P3 2017 python_regexes
P3 2017 python_regexesP3 2017 python_regexes
P3 2017 python_regexes
 
Python String Revisited.pptx
Python String Revisited.pptxPython String Revisited.pptx
Python String Revisited.pptx
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

Strings.ppt

  • 2. Page 1 Agenda • Escape Sequences • Raw Strings • Doc Strings • More formatting • Conversions • String Methods • The string module
  • 3. Page 2 Escapes and Special Characters • Certain special characters have special values • They do not have a corresponding print character • Instead they have an effect • For example, newline, carriage return • To insert a special character, you “escape” it • Using the backslash
  • 4. Page 3 Escape Characters • Newline ('n') • Carriage return ('r') • Single quote (''') • Double quote (""") • Backspace ('b') • Tab ('t') • Vertical tab ('v') • Backslash ('')
  • 5. Page 4 Raw Strings • The raw_input( ) function reads a line from standard input as a raw string • Used in interactive console programs
  • 6. Page 5 Quoting • Can use either single or double • This makes embedded quotes easy • Can also “escape” characters with • Triple quotes define “long strings” • String that can span multiple lines • Often used in documentation and web pages
  • 7. Page 6 str( ) vs. repr( ) • str( ) converts an object to a string • It renders a human-readable string • print uses str( ) (and expands escapes) • repr( ) renders a string that can be evaluated by the interpreter • As if typed at the >>> prompt • So it executes correctly • Also can use backquotes • The interpreter uses repr( ) to echo variables • Differences show up in numbers and escape sequences
  • 8. Page 7 str( ) vs. repr( ) Examples >>> x = .1 >>> x 0.10000000000000001 >>> str(x) '0.1‘ >>> repr(x) '0.10000000000000001‘ >>> `x` '0.10000000000000001‘ >>> s = "Hellonthere" >>> s 'Hellonthere' >>> print s Hello there >>> repr(s) "'Hellonthere'"
  • 9. Page 8 Question • What is the result of executing: str(2+3) ?
  • 10. Page 9 Format Descriptors • %s (string, via str( )) • %r (string, via repr( )) • %c (character) • %d (decimal integer) • %x (hex integer) • %X (uppercase hex) • %e (scientific notation for reals) • %f (fixed-point decimal for reals) • %g (attempts %f; bails to %e)
  • 11. Page 10 String Methods • A lot! • Called as <string-var>.<method>(…) • For example, s.count('a') • capitalize, center, count, endswith, find, index, isalpha, isdigit (etc., as in C), istitle, join, lower, replace, split, strip, swapcase, title, translate, upper, zfill
  • 12. Page 11 Using split( ) • Splits a string into substrings • Uses whitespace as a separator(default) • You can provide your own separator • It uses the whole separator string • See next slide • The re module has a better version of split
  • 13. Page 12 Using split( ) and join( ) Example >>> s = 'a,b,c d,e‘ >>> s.split() ['a,b,c', 'd,e'] >>> s.split(',') ['a', 'b', 'c d', 'e'] >>> ','.join(['x','y','z']) 'x,y,z‘ >>> ''.join(['x','y','z']) 'xyz'
  • 14. Page 13 Exercise • Print the words from a sentence in the reverse order of the appearance, • Write a module that reads a string from the console and tests to see if it is a palindrome (spelled the same backward as forward) (Hint: for both of these, the reverse( ) list method comes in handy)