You are on page 1of 51

ISOM 3400 – Python Programming for Business Analytics

3. Python Basics - String


Yingpeng Robin Zhu

JUN 24, 2022

1
Recap of Last Class

Comments
Variable and Values
Data Types
Operators
Functions
String (e.g., indexing, slicing)

2
Recap of Last Class

Comments
 Writing comments is a good habit (Interpretation of the code )

3
Recap of Last Class

Indentation
 Indicate a block of code

4
Recap of Last Class

Variables & Values


 Rules of variable names, Assign values to variables, …

5
Recap of Last Class

Data Types
 Text (i.e., str), Numeric (e.g., int, float), Sequence (e.g., list, tuple), Boolean (i.e.,
bool), Mapping (i.e., dict)
Data Type Examples
Text Type str “Hi”, “Mike", 'Hello Python’
Numeric Types int 1,2,3
float 5.0, 6.01
complex 5+7j
Sequence Types list ["apple", "banana", "cherry"]
tuple ("apple", "banana", "cherry")
range range(6)
Set set {"apple", "banana", "cherry"}
Mapping Type dict {"name" : "John", "age" : 36}
boolean bool True, False
6
Recap of Last Class

Operations in Python
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

7
Recap of Last Class

String is a very popular data type in Python. We can create strings simply
by enclosing characters in quotes. Single quotation marks and double
quotation marks are the same in Python. 'hello' is the same as "hello".

Creating strings is as simple as assigning a value to a variable

8
Recap of Last Class-String

The characters in a string are indexed by integers


(representing positions in the sequence), and can be
individually accessed by using the indexing operator (Square
brackets [ ] that enclose the index)

9
Class Objectives

String
 Assignment /Concatenate
 Slicing
 String Methods
 Modify Strings
 Format Strings
 Escape Characters
 String Comparison/Membership
 Strings are immutable
Data Structure (If time permits)

10
String Assignment/Concatenate Operator

 The + operator can be used to concatenate two different strings

 Appending the same string to a string can be done using the * operator (Repetition)

11
String Slicing Operator

 Characters from a specific index of the string can be accessed with the string[index]
operator, indexing starts with zero in Python (We will exercise in the lab tasks)

 If we claim a positive number, then start from the left. Remember to start reading
from zero
 The negative indices can be used to start counting from the back. Remember
negative indexing starts reading from -1
 Out of range indexing will give an error message

12
String Slicing Operator

 string[a], returns a character from a positive index a of the string from the left side

 string[-a], returns a character from a negative index a of the string from the right side

 string[a:b], returns characters from positive index a to positive index b, the last
character is not included

13
String Slicing Operator

 string[a:-b], returns characters from positive index a to the negative index b, the last
character is not included

 string[a:], returns characters from positive index a to the end of the string

 string[:b], returns characters from the start of the string to the positive index b

14
String Slicing Operator

 string[-a:-b], returns characters from positive index a to the negative index b, the last
character is not included

15
Modify Strings
 Python has a set of built-in methods that you can use on strings
 Upper Case, the upper() method returns the string in upper case

 Lower Case, the lower() method returns the string in lower case:

 Remove Whitespace, whitespace is the space before and/or after the actual text, and very often you
want to remove this space. The strip() method removes any whitespace from the beginning or the
end

16
Modify Strings
 Python has a set of built-in methods that you can use on strings
 Replace String, The replace() method replaces a string with another string

 What if we run the code below? What information can you get from this code?

17
Modify Strings
 Python has a set of built-in methods that you can use on strings
 Split String, The split() method returns a list where the text between the specified separator
becomes the list items

 What about this one? (We will exercise in the lab tasks)

18
String Format
 We cannot combine strings and numbers as below

19
String Format
 But we can combine strings and numbers by using the format() method
 The format() method takes the passed arguments, formats them, and places them in the string
where the placeholders {} are
 Use the format() method to insert numbers into strings

 Can we miss the placeholders {} ? Try the below code and check whether you can get the same
result:

20
String Format
 But we can combine strings and numbers by using the format() method
 The format() method takes unlimited number of arguments, and are placed into the respective
placeholders

21
String Format
 But we can combine strings and numbers by using the format() method
 You can use index numbers {0} to be sure the arguments are placed in the correct placeholders

Index 0 Index 1 Index 2

 Can you print out the same output with the three variables without using index number?

We will exercise in the lab tasks


22
String Format

 String formatting operator is used to format a string as per requirement, %


 To insert another type of variable along with string, the “%” operator is used along
with python string
 “%” is prefixed to another character indicating the type of value we want to insert
along with the python string

23
String Format

 Another way to do format is by using %


 When referring to multiple variables, a bracket is used. Values are inserted in the order
they appear in the bracket (We will exercise in the lab tasks)

24
Escape Character
 To insert characters that are illegal in a string, use an escape character
 An escape character is a backslash \ followed by the character you want to insert.
 An example of an illegal character is a double quote inside a string that is surrounded by double
quotes, try the code below:

 To fix this problem, use the escape character \"


 The escape character allows you to use double quotes when you normally would not be allowed:

25
Escape Character
 Or you can do in another way, try the below codes:

 What can you conclude from the above codes?

 I encourage you to use escape character rather than try the above code.

We will exercise in the lab tasks

26
String Escape Characters

27
String Comparison Operator

 “==” operator returns Boolean True if two strings are the same and return Boolean
False if two strings are not the same
 “!=” operator returns Boolean True if two strings are not the same and return Boolean
False if two strings are the same

28
String Membership Operator

 “in” or “not in” is used to searching whether the specific character is part/member of
a given input python string

29
Strings are immutable

 It is important to note that strings are constant, immutable values in Python. While
new strings can easily be created it is not possible to modify a string

 One possible solution is to create a new string object with necessary modifications

30
Data Structure

 Lists  Sets
 Indexing of list  Items
 Access Items  Unordered
 Built-in list functions  Unchangeable
 Copying a list  Dictionaries
 List comprehension  Structure
 Tuples  Loop Dictionaries
 Definition  Dictionary Methods
 Functions

31
What is Data Structure?

 Organizing, managing and storing data is important as it enables easier access and
efficient modifications. Data Structures allow you to organize your data in such a way
that enables you to store collections of data, relate them and perform operations on
them accordingly

 In simple terms, data structure refers to the collection or group of data in a particular
structure. In Python, the four common data structures are: Lists, Tuples, Sets, and
Dictionaries

32
What is Data Structure?

33
Lists

 Lists are used to store data of same/different data types in a sequential manner
 Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary, all with different qualities and usage
 There are addresses assigned to every element of the list, which is called as Index
 To create a list, you use the square brackets and add elements into it accordingly

Index 0 1 2 3

Index -4 -3 -2 -1

 Can we create a list to store int/string/boolean… at the same time? Try this:

34
Index 0 1 2 3
Access Items in Lists
Index -4 -3 -2 -1

 List items are ordered, changeable, and allow duplicate values.


 List items are indexed, and you can access them by referring to the index number
 List is ordered. When we say that list is ordered, it means that the items have a defined
order, and that order will not change

35
Index 0 1 2 3
Change Item Value
Index -4 -3 -2 -1

 The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created
 To change the value of a specific item, refer to the index number:

 Change the second value by replacing it with two new values:

36
Change Item Value

 Note: The length of the list will change when the number of items inserted does not
match the number of items replaced
 If you insert less items than you replace, the new items will be inserted where you
specified, and the remaining items will move accordingly
 Try the codes below:

37
Built-in function

 Insert Items: To insert a new list item, without replacing any of the existing values, we
can use the insert() method
 The insert() method inserts an item at the specified index:
 Insert "watermelon" as the third item, try the code below:

38
Built-in function
 To add an item to the end of the list, use the append() method
 The append() method adds an item to the end of the list

39
Built-in function

 You could also use the built-in function to change the list
 The extend() method appends elements from another list to the current list

 The remove() method removes the specified item from the list

40
Built-in function

 You could also use the built-in function to change the list
 The pop() method removes the specified index

 The clear() method empties the list

41
Built-in function

 len(list) returns the number of elements in a list


 max(list) returns the biggest element in a list based on lexicographic order. Can be
used on numeric or string elements
 min(list) returns the smallest element in a list based on lexicographic order. Can be
used on numeric or string elements
 sum(list) returns the sum of all elements in a list. Can only be used on numeric
elements

42
Built-in function

 In a list with string elements, max() and min() are still applicable and return the
first/last element in lexicographical order. The lexicographical order is based on a
character’s ASCII value: 0~9 (48~57); A~Z (65~90); a~z (97~122).(Note that 0~9 here
are not integers, but characters!)

43
Built-in function

 +/in/not in
 Use + to concatenate two lists
 Use in or not in to check if a particular element is inside a list, returns True/False

44
Loop Lists

 You can loop through the list items by using a for loop

45
List Comprehension

 List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list
 Based on a list of names, you want a new list, containing only the names with the
letter "J" in the name
 Without list comprehension you will have to write a for statement with a conditional
test inside:

46
List Comprehension

 With list comprehension you can do all that with only one line of code:

 The Syntax

47
Tuples

 Tuples are similar to lists, but the only big difference is the elements inside a list can
be changed but in tuple it cannot be changed, so tuple is immutable
 Tuples are written with round brackets

48
Why using a tuple instead of a list

 Using a tuple instead of a list guards against accidental modification


 Tuples are good for describing multiple properties of one unchanging thing. E.g., the
parts of a phone number, the coefficients of a polynomial
 The immutability of a tuple makes it a very lightweight data structure. Program
execution is faster when manipulating a tuple than it is for the equivalent list

49
Built-in tuple function

 tuple.count() function counts the number of specified element that is present in the
tuple
 tuple.index() function returns the index of the specified element. If the elements are
more than one, then the index of the first element of that specified element is
returned

50
Jupyter Notebook

51

You might also like