Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1."— 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 single quote ‘ or double quote “ – x = “Larry” – y = ‘Curley’ – Not z = “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 doule quote 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 Helloworld! 01234567891011 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 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

7 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

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, charvariable is the next character in stringvariable Example: – char = “H” – char = “e” – 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 mutable strings – C, C++,… Not legal in languages with immutable strings – Python, Java,… – Usually related to efficiency of string representation


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

Similar presentations


Ads by Google