Lesson2: String Operations, Writing Scripts

Lesson2: String Operations, Writing Scripts

Fundamentals of Text Processing for Linguists Na-Rae Han

Objectives

Writing and executing Python scripts

Learn Python basic syntax

Variable assignments: = vs. += Equality comparison: == String operations Boolean operators: and, or, not

1/15/2014

2

Interactive programming vs. scripts

We have been programming INTERACTIVELY through IDLE

shell.

IDLE interprets and

responds to each line

of code you type in

You have to retype the code next time

We will now try writing and running a stand-alone Python SCRIPT.

x = 'a'+'b'

print x

... ...

Python script file

1/15/2014

executed

Your IDLE shell

3

Python script in IDLE editor mode

1. Open up a new window (Ctrl+n). 2. Python editor window opens up. Type in: print "Hello, world!!" 3. Save the script as "hello.py".

Don't forget to include ".py" extension!

4. Run the script by selecting "Run" > "Run Module" or pressing F5. 5. Success! The command executes in the IDLE shell window.

1/15/2014

4

String operations

String: A single piece of text, composed of a sequence of letters.

Operations on string objects:

print

statement. prints string

len()

returns integer

+, +=

returns string

.endswith(), .startswith() returns True/False

in

returns True/False

.upper(), .lower()

returns string

.replace()

returns string

.split()

splits a string into a list, returns it

1/15/2014

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download