Main Points: - Working with strings. - Problem Solving.

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Strings CS303E: Elements of Computers and Programming.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
12/9/2010 Course A201: Introduction to Programming.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
More Strings CS303E: Elements of Computers and Programming.
Introduction to Computer Science – Chapter 5 CSc 2010 Spring 2011 Marco Valero.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
String and Lists Dr. José M. Reyes Álamo.
Topic: Python Lists – Part 1
String Methods Programming Guides.
Algorithmic complexity: Speed of algorithms
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
Lecture 5 Strings.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 7 STRINGS.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Basic Python Collective variables (sequences) Lists (arrays)
Perl Variables: Array Web Programming.
Creation, Traversal, Insertion and Removal
Chapter 10 Lists.
LING 388: Computers and Language
Strings and Lists – the split method
Data Structures – 1D Lists
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC 221: Introduction to Programming Fall 2018
Programming Training Main Points:
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Programming Training Main Points: - Working with strings.
Algorithmic complexity: Speed of algorithms
Basic String Operations
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Algorithmic complexity: Speed of algorithms
Python Review
Python Strings.
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Understanding Variables
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSE 231 Lab 6.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Main Points: - Working with strings. - Problem Solving. Programming Training Main Points: - Working with strings. - Problem Solving.

Working with Python String General facts about strings: str  datatype for Python strings. A string is a sequence of characters between “”. input() returns string Operations on strings str1 + str2  new strings which concatenates two strings str1 * nr  new string which clones str1 nr times str1 in str2  test if str1 is in str2 Indexing / slicing str is a string with n characters str1[i]  String formed only with character I str1[i1:i2]  Slice of str with all the characters between i1 and i2-1. str1[i1:]  Slice with all the characters from i1

Working with Python String Functions on strings  MUST IMPORT string len(str1)  the length of str1 or number of characters. str1 in str2  finds whether str1 is in str2. str2.count(str1)  counts how many times str1 occurs in str2. str1.split(“ch”)  splits str1 in strings on the ch charter str1.joint(list)  makes a new string by 1) joining all list elements 2) separating them using str1 str1.upper() or str1.lower() or str1.dropcase()  to change the letters case

Working with Python String Functions on strings  MUST IMPORT string string.ascii_letters string.ascii_lowercase string.ascii_uppercase string.punctuation string.digits are default strings with all mention elements.

Invert a String Inverting a string requires to form a new string with the letters of the first one in reverse. Algorithmic approach: Start from an empty string. Traverse the input string in reverse Append the current letter to the new string Python approach rev_str = str1[::-1]

Invert a Sentence Inverting a sentence requires you to invert the order of words in a sentence but preserving the words. Example: “Sabin is teaching the MPT class”  ‘class MPT the teaching is Sabin’ Some questions: 1) how we can acquire the words?  split the string 2) traverse the words in reverse  no problem 3) deal with the ‘.’ if any  just remove it from there e.g. str.remove(‘.’, ‘’) Steps to solve the problem: 1) input the sentence and remove the ‘.’ if there. 2) split the sentence in words. 3) traverse the word list in reverse and add the current word to the new string. 4) add the ‘.’

Invert a Sentence Functions to work with: str2.split() str2.replace(str1, str2) str1 + str2 Better Python solution: ' '.join(str1.replace('.', '').split()[::-1]) Try to understand?

Counting Stuff in a String This requires you to find how many characters are alphabetical, numerical or space. Is str1 is a single character string then you can test its nature str1.isalpha() str1.isdigit() str1.isspace() etc It is a simple counting problem in which: Initialise the counting variables Traverse the string Test the nature of the current character and count

To do List Solve the HW problems. Read more about Strings