Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Advertisements

Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Introduction to Python
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
String Escape Sequences
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Strings.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
COMPSCI 101 Principles of Programming Lecture 4 –The type() function, the string type, the len() function, string slices.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Strings CS303E: Elements of Computers and Programming.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Chapter 2 Variables.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
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.
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.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
Introduction to Programming
String Processing Upsorn Praphamontripong CS 1110
Section 3.2c Strings and Method Signatures
Introduction to Strings
Introduction to Strings
Learning to Program in Python
MSIS 655 Advanced Business Applications Programming
Introduction to C++ Programming
Bryan Burlingame 17 October 2018
Introduction to Programming
“If you can’t write it down in English, you can’t code it.”
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
16 Strings.
Introduction to Strings
CS1110 Today: collections.
Bryan Burlingame 13 March 2019
Introduction to Computer Science
String methods 26-Apr-19.
Introduction to Strings
String Class.
Introduction to Strings
Unit-2 Objects and Classes
Presentation transcript:

Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1

Strings as a Single Value Basic operators: – Assignment: name = “Fred” name1 = name – Comparison: if name == “Fred” if name1 != “Barney” – Concatenation: fullname = name + “ Flintstone” – Repetition: name3 = name * 3

String Literals Quotes used to indicate string literal – x is a variable, “x” is a string – 123 is a number, “123” is a string Can use single quote ‘ or double quote “ – x = “Larry” – y = ‘Curley’ – Not z = “Moe’ – Should use one form consistently

Escape Characters Syntax: \somecharacter Spacing characters: \t inserts tab \n inserts new line Inserting quotes into strings without having them treated like beginning/end of string literal: \’ inserts single quote \” inserts doule quote Example: print(“He said \”Hello\””)  He said “Hello”

Strings as Lists of Characters Strings can be broken down into their individual characters Each character in a string has an index – First index is 0 Helloworld! index

Strings as Lists of Characters Accessing individual characters: Syntax: stringvariable[index] Example: greeting = “Hello world!” print(greeting[7])  ‘r’ Note: Index must be legal print(greeting[12])  error! Can use len function to find length of string len(greeting)  12 – Note: Highest index = length – 1 since first index is 0

Strings as Lists of Characters Accessing substring of characters – All characters between start index and end index Syntax: stringvar[start:end] – Example: print(greeting[3:7])  ‘lo wor’ Default syntax: stringvar[start:] – All chars from start to end of string stringvar[:end] – All chars from start of string to end

Strings and Loops Loops often used to process strings – Loop counter = index in string Example: Printing all leading substrings of a given word Strategy: – Use stringvar[:end] to print first end characters – Use for loop to vary end from 1 to length of word Use len function to find length of word

Strings and Loops

For Loops and Strings Special form of for loop for strings for charvariable in stringvariable: – Each time through loop, charvariable is the next character in stringvariable Example: – char = “H” – char = “e” – char = “l” – char = “o” – char = “!”

For Loops and Strings Example: “Exploding” a word by inserting a space between each letter Strategy: – Use loop to get each letter – Print it followed by a space (instead of a return)

String Mutability Can index be used to change a string? name = “Larry” name[1] = “o”  name now “Lorry” Legal in languages with mutable strings – C, C++,… Not legal in languages with immutable strings – Python, Java,… – Usually related to efficiency of string representation