Professional Documents
Culture Documents
8 - Data Structures (Strings) - ST
8 - Data Structures (Strings) - ST
8 - Data Structures (Strings) - ST
DATA STRUCTURES -8
(STRINGS)
Basimah Aljhne
Click icon to add picture
1. The string Type
2. String Operations
3. String methods and functions
4. Example
THE STRING TYPE Click icon to add picture
------------------------------------------------------------------
------------------------------------------------------------------
The function chr()
------------------------------------------------------------------
------------------------------------------------------------------
Question from 1 to 4
How to create a string in Python?
There are two ways to declare a f you want to write string on
string written on a line by using multiple lines using a pair of 3
single quotes or double quotes. single quotes.
Index syntax?
Sring_var [ ]
>>> hello_string[6:]
>>> ‘Wold’
>>> hello_string[:5]
>>> ‘Hello’
Slicing _ Negative indices
>>> hello_string[-1]
>>> ‘d’
>>> hello_string[3:-2]
>>> ‘lo wor’
Extended Slicing
defaults are:
start is beginning, finish is end, steps is 1
>>> hello_string[ : : 2]
Copy Slice
How to make a copy of a string:
my_str = 'hi mom'
new_str = my_str[:]
reverseStr = my_str[::-1]
How to access characters in a string? Using index
Using Slicing
Slicing syntax?
Sring_var [ : : ]
Start index
Finish index
steps
How to change or delete a string?
Value
Indexing
slicing
Immutable
What happens when you invoke each of these commands؟
greet[1]
greet[1:8]
greet = "Have a great day"
greet[8: ]
greet[-5]
Greet[-7:-3]
The + operator does this in Python. Simply writing two string literals together also
concatenates them.
The * operator can be used to repeat the string for a given number of times.
Concatenation (+) and Repetition (*)
There are many operations that can be performed with string which makes it one of
the most used datatypes in Python.
both + and * on strings makes a new string, does not modify the arguments.
Whole strings:
Compare the first element of each string
if they are equal, move on to the next character in each
if they are not equal, the relationship between those to characters are the
relationship between the string
if one ends up being shorter (but equal), the shorter is smaller
Examples:
'aaab' < ‘aaac' True
‘aa' < ‘aaz' True
my_str = 'aabbccdd'
Value
Repetition (*)
slicing
Comparison Operators
Immutable Iterating Through String
1)counter
2)Tool that iterate
through string
STRING METHODS AND Click icon to add picture
FUNCTIONS
1. String Method vs Function
2. Python String Methods
3. Built-in functions to Work with Python
String Method vs Function
function method
is a program that performs some a method is a variation on a function
operation. Its details are hidden like a function, it has input arguments
(encapsulated), only it's interface and an output
provided. Unlike a function, it is applied in the
A function takes some number of inputs context of a particular object
(arguments) and returns a value based This is indicated by the dot notation
on the arguments and the function's invocation
operation. Example:
Example: upper is the name of a method. It
generates a new string that has all upper
The len function takes as an argument a case characters of the string it was called
string and returns an integer, the length of with.
a string
my_str = 'Python Rules!'
my_str = 'Hello World’
my_str.upper()'PYTHON RULES!'
len(my_str) 11 # space counts
Python String Methods
Chaining methods
Methods can be chained my_str = 'Python Rules!’
together. my_str.upper() 'PYTHON RULES!'
Perform first operation, yielding an my_str.upper().find('O') 4
object
Use the yielded object for the next
method
)( Find
my_str = 'hello'
my_str.find('l') # find index of 'l' in my_str
2
• Note how the method 'find' operates on the string object my_str and the two are
associated by using the “dot” notation: my_str.find('l').
• Terminology: the thing(s) in parenthesis, i.e. the 'l' in this case, is called an
argument
Python String Methods
Optional Arguments
• Some methods have optional arguments: a_str = 'He had the bat'
• if the user doesn't provide one of these, a_str.find('t’) 7
a default is assumed # 1st 't',start at 0
a_str.find('t',8) 13
• For example: find has a default second
# 2nd 't'
argument of 0, where the search begins
)( split
The split method will take a string and break it into multiple new string parts
depending on the argument character.
by default, if no argument is provided, split is on any whitespace character
(tab, blank, etc.)
you can assign the pieces with multiple assignment if you know how many pieces
are yielded
lower( )
This method returns a copy of the string in which all case-based characters have been lowercased.
replace()
Following is the syntax for replace() method str.replace(old, new[, max])
Parameters:
old − This is old substring to be replaced.
new − This is new substring, which would replace old substring.
max − If this optional argument max is given, only the first count occurrences are replaced.
• You can use IDLE to find available methods for any type.
• You enter a variable of the type, followed by the '.' (dot) and then a tab
• Remember, methods match with a type. Different types have different methods
• If you type a method name, IDLE will remind you of the needed and optional
arguments.
Your Turn complete
it
Built-in functions to Work with Python
• Various built-in functions that work with sequence, works with string as well
• Some of the commonly used ones is len().
• len() returns the length (number of characters) of the string.
What doing Function
takes a character and returns its UTF-8 integer ord(‘a’)
value
takes an integer and returns the UTF-8 chr(97)
.character
takes an object and return the number of its len(object)
items
Return the index of an element and the element enumerate(iterable, start=0)
itself
Value