1 of 19

ME 4990: Intro to CS�Object-Oriented Programming & Machine Learning 101

String Op, Functions

2 of 19

Outline

  • String, AD Chapter 8
    • Basic Operations
    • File Reading!!!

3 of 19

String

4 of 19

String: AD chapter 8

  • String is a list in term of indexing
  • Again, in python, index starts at 0

5 of 19

String: length

  • A sequence is a list
  • Length of list can be found by:

6 of 19

Traversal of string

  • Use for loop

7 of 19

String slicing

  • Again, string is like a list in term of indexing

8 of 19

You cannot change a string element

  • String is not a simple list, it is immutable

9 of 19

String methods

    • find() and index()
    • count()
    • split()
    • Convert to float and int

10 of 19

find() and index() method

  • Example code: find_method.py

11 of 19

find() and index() method

  • You can specify start and end position

12 of 19

Just a trick

  • What will be the output?

13 of 19

count()

  • Find how many times a string repeats

14 of 19

split(): see split_method.py

15 of 19

Convert to float or int

16 of 19

Convert to float or int

17 of 19

Table file reading

18 of 19

Table file reading

  • Example: file_reading.py

19 of 19

Special characters

  • Special characters: