0% found this document useful (0 votes)
6 views

Unit-3 - text and file processing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit-3 - text and file processing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Text and File Processing

67
Strings
 string: A sequence of text characters in a program.
 Strings start and end with quotation mark " or apostrophe ' characters.
 Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"

 A string may not span across multiple lines or contain a " character.
"This is not
a legal String."
"This is not a "legal" String either."

 A string can represent characters by preceding them with a backslash.


 \t tab character
 \n new line character
 \" quotation mark character
 \\ backslash character

 Example: "Hello\tthere\nHow are you?"

68
Indexes
 Characters in a string are numbered with indexes starting at 0:
 Example:
name = "P. Diddy"

index 0 1 2 3 4 5 6 7
character P . D i d d y

 Accessing an individual character of a string:


variableName [ index ]

 Example:
print name, "starts with", name[0]

Output:
P. Diddy starts with P

69
String properties
 len(string) - number of characters in a string
(including spaces)
 str.lower(string) - lowercase version of a string
 str.upper(string) - uppercase version of a string

 Example:
name = "Martin Douglas Stepp"
length = len(name)
big_name = str.upper(name)
print big_name, "has", length, "characters"

Output:
MARTIN DOUGLAS STEPP has 20 characters

70
raw_input
 raw_input : Reads a string of text from user input.
 Example:
name = raw_input("Howdy, pardner. What's yer name? ")
print name, "... what a silly name!"
Output:
Howdy, pardner. What's yer name? Paris Hilton
Paris Hilton ... what a silly name!

71
Text processing
 text processing: Examining, editing, formatting text.
 often uses loops that examine the characters of a string one by one

 A for loop can examine each character in a string in sequence.


 Example:
for c in "booyah":
print c
Output:
b
o
o
y
a
h

72
Strings and numbers
 ord(text) - converts a string into a number.
 Example: ord("a") is 97, ord("b") is 98, ...

 Characters map to numbers using standardized mappings such as


ASCII and Unicode.

 chr(number) - converts a number into a string.


 Example: chr(99) is "c"

 Exercise: Write a program that performs a rotation cypher.


 e.g. "Attack" when rotated by 1 becomes "buubdl"

73
File processing
 Many programs handle data, which often comes from files.

 Reading the entire contents of a file:


variableName = open("filename").read()

Example:
file_text = open("bankaccount.txt").read()

74
Line-by-line processing
 Reading a file line-by-line:
for line in open("filename").readlines():
statements

Example:
count = 0
for line in open("bankaccount.txt").readlines():
count = count + 1
print "The file contains", count, "lines."

 Exercise: Write a program to process a file of DNA text, such as:


ATGCAATTGCTCGATTAG
 Count the percent of C+G present in the DNA.

75

You might also like