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

chapt 4 Python courses

The document outlines a course on Computer Applications in Industrial Engineering, focusing on string data types and sequences in Python. It covers string operations, indexing, slicing, and the use of lists, emphasizing their differences and applications in programming. Additionally, it introduces string and list methods for manipulating textual data and provides examples for practical understanding.

Uploaded by

AB
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)
7 views

chapt 4 Python courses

The document outlines a course on Computer Applications in Industrial Engineering, focusing on string data types and sequences in Python. It covers string operations, indexing, slicing, and the use of lists, emphasizing their differences and applications in programming. Additionally, it introduces string and list methods for manipulating textual data and provides examples for practical understanding.

Uploaded by

AB
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/ 11

Course

Computer Applications in Industrial Engineering (1)


Title:

Course Code: 8053101-3

Program: Industrial Engineering Program

Department: Mechanical Engineering Department

College: College of Engineering

Institution: Taif University


Chapter 4: Sequences: Strings and Lists

1) Objectives

• To understand the string data type and how strings are represented in the computer.
• To become familiar with various operations that can be performed on strings through
built-in functions and string methods.
• To understand the basic idea of sequences and indexing as they apply to Python strings
and lists.
• To be able to apply string formatting to produce attractive, informative program
output.

2) The String Data Type

This chapter focuses on textual applications to introduce some important ideas about how
text is stored on the computer.

In Chapter 2 you learned that a string literal is formed by enclosing some characters in
quotation marks. Python also allows strings to be delimited by single quotes (apostrophes).
There is no difference; just be sure to use a matching set. Strings can also be saved in variables,
just like any other data. Here are some examples illustrating the two forms of string literals:
You can think of a string as a sequence of characters.

When you want to get a string, you can use the input in its "raw'' (unconverted) form. Here's
a simple interaction to illustrate the point:
For starters, remember what a string is: a sequence of characters. One thing we might want
to do is access the individual characters that make up the string. In Python, this can be done
through the operation of indexing. We can think of the positions in a string as being numbered,
starting from the left with 0. The general form for indexing is <string> [ <expr >]. The value of
the expression determines which character is selected from the string.

-9 -8 -7 -6 -5 -4 -3 -2 -1

Here are some interactive indexing examples:

Notice that in a string of n characters, the last character is at position n- 1, because the
indexes start at 0.
This is probably also a good time to remind you about the difference between string
objects and the actual printed output. In the interactions above, the Python shell shows us the
value of strings by putting them in single quotes; that's Python's way of communicating to us
that we are looking at a string object. When we actually print the string, Python does not put
any quotes around the sequence of characters. We just get the text contained in the string.
By the way, Python also allows indexing from the right end of a string using
negative indexes.
It is possible to access a contiguous sequence of characters or substring from a string. In
Python, this is accomplished through an operation called slicing. You can think of slicing as a
way of indexing a range of positions in the string. Slicing takes the form [ : ]. Both start and
end should be int -valued expressions. A slice produces the substring starting at the position
given by start and running up to, but not including, position end. Continuing with our
interactive example, here are some slices:

Here are some examples of various string operations:

These basic string operations are summarized in following Table:


Table 4.1

3) simple string processing

Example 1:

Our first example is a program to compute the usernames for a computer system. Many
computer systems use a username and password combination to authenticate system users.
The system administrator must assign a unique username to each user. Often, usernames
are derived from the user's actual name. One scheme for generating usernames is to use the
user's first initial followed by up to seven letters of the user's last name. Using this method,
the username for John Smith would just be 'JSmith."

-------------------------
This program generates computer usernames .
Please enter your first name (all lowercase) : malek
Please enter your last name (all lowercase) : njah
Your username is: mnjah

Putting the newline character (\n) at the end of the string in the first print statement caused
the output to skip down an extra line. This is a simple trick for putting some extra white space
into the output to make it look a little better.
Example 2:

Suppose we want to print the abbreviation of the month that corresponds to a given month
number. The input to the program is an int that represents a month number ( 1- 12), and the
output is the abbreviation for the corresponding month. For example, if the input is 3, then
the output should be Mar, for March.
The basic idea is to store all the month names in a big string:

We can look up a particular month by slicing out the appropriate substring. The trick is
computing where to slice. Since each month is represented by three letters, if we knew where
a given month started in the string, we could easily extract the abbreviation:

How do we compute this position? Let's try a few examples and see what we find. Remember
that string indexing starts at 0.

(1-1)*3=0
(2-1)*3=3
(n-1)*3=position
(3-1)*3=6
(4-1)*3=9

Of course, the positions all turn out to be multiples of 3. To get the correct multiple, we just
subtract 1 from the month number and then multiply by 3. So for 1 we get (1-1) * 3 = 0 * 3 =
0, and for 12 we have (12 -1) * 3 = 11 * 3 = 33. Now we're ready to code the program. Again,
the final result is short and sweet; the comments document the algorithm we've developed.
Here is a sample of program output:

4) Lists as Sequences

The operations in Table 4. 1 are not really just string operations. They are operations that
apply to sequences. We can also index, slice, and concatenate lists, as the following session
illustrates:
One of the nice things about lists is that they are more general than strings. Strings are always
sequences of characters, whereas lists can be sequences of arbitrary objects.
You can even mix it up and create a list that contains both numbers and strings:

Using a list of strings, we can rewrite our month abbreviation program from the previous
section and make it even simpler:

Lists, just like strings, are indexed starting with 0, so in this list the value months [0] is the
string " Jan".
While strings and lists are both sequences, there is an important difference between the two.
Lists are mutable. That means that the value of an item in a list can be modified with an
assignment statement. Strings, on the other hand, cannot be changed "in place." Here is an
example interaction that illustrates the difference:
5) String Methods

we will make use of the split method. This method splits a string into a list of substrings. By
default, it will split the string wherever space occurs. Here's an example:

Naturally, the split operation is called using the usual dot notation for invoking one of an
object's methods. In the result, you can see how split has turned the original string "Hello,
string methods! " into a list of three substrings: "Hello, " , " string " , and "methods ! ".
By the way, split can be used to split a string at places other than spaces by supplying the
character to split on as a parameter. For example, if we have a string of numbers separated
by commas, we could split on the commas:

This is useful for getting multiple inputs from the user without resorting to the use of eval. For
example, we could get the x and y values of a point in a single input string, turn it into a list
using the split method, and then index into the resulting list to get the individual component
strings as illustrated in the following interaction:
Python is a very good language for writing programs that manipulate textual data.

Python also has a number of other standard libraries for text processing that are not covered
here. You can consult the online documentation or a Python reference to find out more.
6)Lists Methods

Like strings, lists are also objects and come with their own set of "extra" operations.

In this example we start with an empty list ([]) and each number from 1 to 100 is squared and
appended to the list. When the loop is done, squares will be the list: [ 1 , 4, 9, . . . , 10000] .

You might also like