PythonStringsNotes (1) - Strings Some basics on Strings Strings can be ONE or more characters of text strung together o\"c is a string o\"Mr Lupoli is da

PythonStringsNotes (1) - Strings Some basics on Strings...

This preview shows page 1 - 6 out of 21 pages.

Strings Some basics on Strings Strings can be ONE or more characters of text strung together o "c" is a string o "Mr. Lupoli is da man!" is a string o "123-213-1232" is a string o "123123123" is a string To create a string o notice when using Strings "" are used (but you can use '' too) Strings are immutable. Lists are mutable. o means they cannot be changed o will require more work to change the value, but can be done o Strings require a “=” to RESET their value # equals sign (i.e. assignment) is not just used for math name = "Lupoli" two = name Index(ices) [0] [1] [2] [3] [4] [5] L u p o l i name # notice a String starts at index 0!! String Declaration and Initialization strings are sequences o each element contains a character (single letter) together they make a String Assigning values o Must use " "'s to set if literal o variable = input("Please enter.. ") reads the whole line typed. don't have to worry about declaring a SIZE that fits!! (automatically done!!) No special treatment, just like any other variable o notice how I gave them values below Can use + to concatenate strings together (not other types too) 1
Image of page 1
String Declaration and Initialization Gathering values into Strings Example 1 firstName = "" lastName = "" # input will be: Mr. Lupoli firstName = input("Please enter your first name: ") lastName = input("Please enter your last name: ") print(lastName + ", " + firstName) # output: Lupoli, Mr. firstName lastName 0 1 2 M r . 0 1 2 3 4 5 L u p o l i Gathering values into Strings Example 2 firstName = "Kristen" lastName = "Davis" print(lastName + ", " + firstName) # output: Davis, Kristen firstName lastName K r i s t e n D a v i s Gathering values into Strings Example 3 wholeName = "" # input: Mr. Lupoli wholeName = input("Please enter your WHOLE name.") print(wholeName) # output: Mr. Lupoli wholename 2
Image of page 2
0 1 2 3 4 5 6 7 8 9 M r . L u p o l i 3
Image of page 3
Overall String operators python has some nice operators that are easy to use in String manipulation Operation Python Operator Concatenation + Repetition * Indexing [ ] Slicing [ : ] Python String Operators in Action Slicing print('hello'[1:4]) "ell" name = "Lupoli" sample = "Redmond" print(name[3:]) "oli" print(name[:3]) "Lup" Indexing print(sample[-1]) 'd' print(sample[5]) 'n' print(sample[-3]) 'o' print(name[0]) 'L' Concatenation print('snow' + 'ball') 'snowball' Repetition print('hello' * 3) 'hellohellohello' print('hello ' * 3 + '!') 'hello hello hello !' print("ABcdEF"[2:4] * 4) ‘cdcdcdcd’ (Thanks Alexander Bauer F13) 4
Image of page 4
Concatenation adds to END of existing String o the new string object will be the old string and the new string together Code Visually string1 = "Goodbye" string2 = ", Cruel " string1 = string1 + string2 string1 = string1 + "World!" # literal string string1 G o o d b y e string1 G o o d b y e , _ C r u e l _ string1 G o o d b y e , C r u e l W o r Determine what "answer" will look like after the code below WITHOUT A Interperter!!! Draw it out!!! 1. string1 = "Goodbye" # drawn below 2. string2 = ", Cruel " 3. answer = "" 4. answer = answer + string1 5. answer += string2 # hmmm, think about it!!
Image of page 5
Image of page 6

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture