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

Lecture 3@StringDataStructure

Uploaded by

hudaisa wazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 3@StringDataStructure

Uploaded by

hudaisa wazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

String Data Structure

Ghulam Abbas
What is String in Python?

A string is a sequence of characters.


A character is simply a symbol. For example, the English
language has 26 characters.
Computers do not deal with characters, they deal with
numbers (binary). Even though you may see characters on
screen, internally it is stored and manipulated as a
combination of 0's and 1's.
What is String in Python?

 Conversion of character to a number is called encoding,


and the reverse process is decoding.
 ASCII and Unicode are some of the popular encoding used.
 In Python, a string is a sequence of Unicode characters.
 Unicode was introduced to include every character in all
languages and bring uniformity in encoding.
How to create a string in Python?
 Strings can be created by enclosing characters inside a
single quote or double-quotes.
 Even triple quotes can be used in Python but generally used
to represent multiline strings.

Output
How to access characters in a string?
We can access individual characters using indexing and a
range of characters using slicing.Index starts from 0.
 Trying to access a character out of index range will raise
an IndexError.
 The index must be an integer.
We can't use float or other types, this will result into TypeError.
Output
How to access characters in a string?
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last
item and so on.
We can access a range of items in a string by using the
slicing operator (colon).
Output
Syntax Errors
 If we try to access index out of the range or use decimal
number, we will get errors.
Slicing
 To extract substring from the whole string then we use the
syntax like
beginning represents the starting index of string
end denotes the end index of string which is not inclusive
steps denotes the distance between the two words.
Note: We can also slice the string using beginning and only
and steps are optional.
 Slicing can be best visualized by considering the index to be
between the elements as shown below.
 If we want to access a range, we need the index that will
slice the portion from the string.
Example
How to change or delete a string?
Strings are immutable. This means
We cannot delete or remove
that elements of a string cannot be
characters from a string.
changed once it has been
But deleting the string entirely is
assigned. We can simply reassign
possible using the keyword del.
different strings to the same name.
Updating Strings
 "update" an existing string by (re)assigning a variable to
another string.
 The new value can be related to its previous value or to a
completely different string altogether. For example
Python String Methods

 Set 1 (find, rfind, startwith, endwith, islower, isupper, lower,


upper, swapcase & title)

 Set 2 (len, count, center, ljust, rjust, isalpha, isalnum,


isspace & join)

 Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate,


replace & expandtabs())
Set 1 (find & rfind)
 find(“string”, beg, end) :- This function is used
to find the position of the substring within a string.
It takes 3 arguments, substring , starting index(
by default 0) and ending index( by default
string length).
It returns “-1 ” if string is not found in given range.
It returns first occurrence of string if found.

 rfind(“string”, beg, end) :- This function has


the similar working as find(), but it returns the
position of the last occurrence of string.
Startswith & endswith
 startswith(“string”, beg, end) :- The purpose
of this function is to return true if the
function begins with mentioned
string(prefix) else return false.

 endswith(“string”, beg, end) :- The purpose


of this function is to return true if the function
ends with mentioned string(suffix) else
return false.
Islower & isupper
 islower(“string”) :- This function
returns true if all the letters in the
string are lower cased, otherwise
false.
 isupper(“string”) :- This function
returns true if all the letters in the
string are upper cased, otherwise
false.
Lower, upper, swapcase & title
 lower(): This function returns the new string with all the letters converted into
its lower case.
 upper(): This function returns the new string with all the letters converted into
its upper case.
 swapcase(): This function is used to swap the cases of string i.e upper case is
converted to lower case and vice versa.
 title() :This function converts the string to its title case i.e the first letter of every
word of string is upper cased and else all are lower cased.
Example

Output
Set-2 len & count
 len() : This function returns the length of the string. :- This function returns
the length of the string.
 count(“string”, beg, end): This function counts the occurrence of
mentioned substring in whole string. This function takes 3 arguments,
substring, beginning position( by default 0) and end position(by default
string length).
Center, ljust & rjust
 center():This function is used to surround the string with a character repeated
both sides of string multiple times. By default the character is a space. Takes 2
arguments, length of string and the character.
 ljust(): This function returns the original string shifted to left that has
a character at its right. It left adjusts the string. By default the character is space.
It also takes two arguments, length of string and the character.
 rjust(): This function returns the original string shifted to right that has
a character at its left. It right adjusts the string. By default the character is space.
It also takes two arguments, length of string and the character.
example
Isalpha, isalnum & isspace
 isalpha() :- This function returns true
when all the characters in the string
are alphabets else returns false.
 isalnum() :- This function returns true
when all the characters in the string
are combination of numbers and/or
alphabets else returns false.
 isspace() :- This function returns true
when all the characters in the string
are spaces else returns false
join
join(): This function is used
to join a sequence of strings
mentioned in its arguments
with the string.
Set 3: strip, lstrip, rstrip
 strip(): This method is used to delete
all the leading and
trailing characters mentioned in its
argument.
 lstrip(): This method is used to delete
all the leading characters mentioned
in its argument.
 rstrip(): This method is used to delete
all the trailing characters mentioned
in its argument.
Min &max

 min(“string”): This function


returns the minimum value
alphabet from string.
 max(“string”): This function
returns the maximum value
alphabet from string

You might also like