SlideShare a Scribd company logo
An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #13:
Processing Strings
Processing Strings
• One of the main usages of loops is to process strings.
• Examples:
– Count the occurrence of one or more characters in a string
– Verify that the contents of a string meet a criteria
• All of this requires basic string processing algorithms
4 String Processing Methods
• variable.isupper
– Test to see if current character is upper case
– Returns True or False
• variable.islower
– Test to see if current character is lower case
– Returns True or False
• variable.upper
– Converts current character to upper case
• variable.lower
– Converts current character to lower case
Counting Matches
• We often want to count the
number of values that meet
a given condition.
• Example: count the number
of upper case characters in
a string
uppercase = 0
for char in string :
if char.isupper() :
uppercase =
uppercase + 1
• Sometimes, you need to count
the number of occurrences of
multiple characters
within a string.
• Example, suppose we would like
to know how many vowels are
contained in a word.
vowels = 0
for char in word :
if char.lower() in "aeiou" :
vowels = vowels + 1
a & b
Note: using “.lower” allows us to limit the
number of characters that must be specified
in the literal string
Finding All Matches
• You may need to find the position of each match within a string. For example,
suppose you are asked to print the position of each uppercase letter in a sentence.
• You cannot use the for statement that iterates over all characters because you
need to know the positions of the matches. Instead, iterate over the positions
(using for with range) and look up the character at each position:
sentence = input("Enter a sentence: ")
for i in range(len(sentence)) :
if sentence[i].isupper() :
print(i)
What We Covered Today
1. Tables
1. Creating
2. Accessing
3. Neighbors
4. Summing
2. List Algorithms
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. Lists, List algorithms
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

More Related Content

PDF
Regular expressions
PPTX
Regular expressions
PPTX
Bioinformatica p2-p3-introduction
PPTX
Using Regular Expressions in Grep
PPT
Regular Expressions grep and egrep
PPT
Introduction to Regular Expressions
PDF
Intro to Python Workshop San Diego, CA (January 19, 2013)
PDF
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Regular expressions
Regular expressions
Bioinformatica p2-p3-introduction
Using Regular Expressions in Grep
Regular Expressions grep and egrep
Introduction to Regular Expressions
Intro to Python Workshop San Diego, CA (January 19, 2013)
Python for Image Understanding: Deep Learning with Convolutional Neural Nets

Similar to An Introduction To Python - Processing Strings (20)

PPTX
Regex lecture
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
PDF
Course 102: Lecture 13: Regular Expressions
PPTX
ADST university of Sussex foundation class
PDF
Don't Fear the Regex LSP15
PPTX
Python_Unit_III.pptx
PPTX
UNIT 4 python.pptx
PPT
Scala Language Intro - Inspired by the Love Game
DOCX
What is the general format for a Try-Catch block Assume that amt l .docx
PDF
Construction of a predictive parsing table.pdf
PDF
Don't Fear the Regex - Northeast PHP 2015
PDF
Don't Fear the Regex WordCamp DC 2017
PDF
Introduction_to_Regular_Expressions_in_R
PPT
2.regular expressions
PPTX
Chapter 3: Introduction to Regular Expression
PDF
Ruby cheat sheet
PPT
PERL Regular Expression
PPTX
Python ppt_118.pptx
PPTX
NLP_KASHK:Regular Expressions
Regex lecture
Don't Fear the Regex - CapitalCamp/GovDays 2014
Course 102: Lecture 13: Regular Expressions
ADST university of Sussex foundation class
Don't Fear the Regex LSP15
Python_Unit_III.pptx
UNIT 4 python.pptx
Scala Language Intro - Inspired by the Love Game
What is the general format for a Try-Catch block Assume that amt l .docx
Construction of a predictive parsing table.pdf
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex WordCamp DC 2017
Introduction_to_Regular_Expressions_in_R
2.regular expressions
Chapter 3: Introduction to Regular Expression
Ruby cheat sheet
PERL Regular Expression
Python ppt_118.pptx
NLP_KASHK:Regular Expressions
Ad

Recently uploaded (20)

PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Presentation on HIE in infants and its manifestations
PPTX
master seminar digital applications in india
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Chinmaya Tiranga quiz Grand Finale.pdf
Final Presentation General Medicine 03-08-2024.pptx
Institutional Correction lecture only . . .
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial disease of the cardiovascular and lymphatic systems
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
102 student loan defaulters named and shamed – Is someone you know on the list?
Presentation on HIE in infants and its manifestations
master seminar digital applications in india
Ad

An Introduction To Python - Processing Strings

  • 1. An Introduction To Software Development Using Python Spring Semester, 2014 Class #13: Processing Strings
  • 2. Processing Strings • One of the main usages of loops is to process strings. • Examples: – Count the occurrence of one or more characters in a string – Verify that the contents of a string meet a criteria • All of this requires basic string processing algorithms
  • 3. 4 String Processing Methods • variable.isupper – Test to see if current character is upper case – Returns True or False • variable.islower – Test to see if current character is lower case – Returns True or False • variable.upper – Converts current character to upper case • variable.lower – Converts current character to lower case
  • 4. Counting Matches • We often want to count the number of values that meet a given condition. • Example: count the number of upper case characters in a string uppercase = 0 for char in string : if char.isupper() : uppercase = uppercase + 1 • Sometimes, you need to count the number of occurrences of multiple characters within a string. • Example, suppose we would like to know how many vowels are contained in a word. vowels = 0 for char in word : if char.lower() in "aeiou" : vowels = vowels + 1 a & b Note: using “.lower” allows us to limit the number of characters that must be specified in the literal string
  • 5. Finding All Matches • You may need to find the position of each match within a string. For example, suppose you are asked to print the position of each uppercase letter in a sentence. • You cannot use the for statement that iterates over all characters because you need to know the positions of the matches. Instead, iterate over the positions (using for with range) and look up the character at each position: sentence = input("Enter a sentence: ") for i in range(len(sentence)) : if sentence[i].isupper() : print(i)
  • 6. What We Covered Today 1. Tables 1. Creating 2. Accessing 3. Neighbors 4. Summing 2. List Algorithms Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
  • 7. What We’ll Be Covering Next Time 1. Lists, List algorithms Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

Editor's Notes

  • #2: New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.