BIO
PythonStringsNotes (1)

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

Info icon This preview shows pages 1–6. Sign up to view the full content.

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

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '14
  • Higgens
  • ASCII, Shawn Lupoli

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern