Introduction to strings
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Introduction to Strings PowerPoint PPT Presentation


  • 69 Views
  • Uploaded on
  • Presentation posted in: General

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”

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to strings

Introduction to Strings

CSIS 1595: Fundamentals of Programming and Problem Solving 1


Strings as a single value

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

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


Escape characters

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”


Strings as lists of characters

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


Strings as lists of characters1

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


Strings as lists of characters2

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


Strings and loops

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 loops1

Strings and Loops


For loops and strings

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 = “!”


For loops and strings1

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

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


  • Login