1 / 12

Introduction to Strings

Introduction to Strings. CSIS 1595: Fundamentals of Programming and P roblem 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”

tirzah
Download Presentation

Introduction to Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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 singlequote ‘ or doublequote “ • x = “Larry” • y = ‘Curley’ • Notz = “Moe’ • Should use one form consistently

  4. 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 doulequote Example: print(“He said \”Hello\””) He said “Hello”

  5. 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 index

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

  7. Strings as Lists of Characters • Accessing substring of characters • All characters between startindex 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

  8. 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

  9. Strings and Loops

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

  11. 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)

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

More Related